Skip to content

Instantly share code, notes, and snippets.

View onevcat's full-sized avatar
🐭

Wei Wang onevcat

🐭
View GitHub Profile
@onevcat
onevcat / uuid.m
Created April 23, 2012 08:27
UUID
- (NSString *)createUUID
{
// Create universally unique identifier (object)
CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
// Get the string representation of CFUUID object.
NSString *uuidStr = [(NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject) autorelease];
// If needed, here is how to get a representation in bytes, returned as a structure
// typedef struct {
@onevcat
onevcat / UIColorFromHex.m
Created May 10, 2012 03:06 — forked from jamztang/UIColorFromHex.m
Convert hex value to UIColor
UIColor* UIColorFromHex(NSInteger colorInHex) {
// colorInHex should be value like 0xFFFFFF
return [UIColor colorWithRed:((float) ((colorInHex & 0xFF0000) >> 16)) / 0xFF
green:((float) ((colorInHex & 0xFF00) >> 8)) / 0xFF
blue:((float) (colorInHex & 0xFF)) / 0xFF
alpha:1.0];
}
@onevcat
onevcat / err_arc.m
Created June 2, 2012 15:19
error cast in arc conversion
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
if (fileURL != nil)
{
SystemSoundID theSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)fileURL, &theSoundID);
if (error == kAudioServicesNoError)
soundID = theSoundID;
}
@onevcat
onevcat / gist:3254692
Created August 4, 2012 05:07
Find and replace the email address in plain text with Perl
use Email::Find;
sub replaceEmail {
my $data = shift @_;
my $finder = Email::Find->new(
sub {
my($email, $orig_email) = @_;
my($address) = $email->format;
return qq|<a href="mailto:$address">$orig_email</a>|;
}
);
@onevcat
onevcat / gist:3254713
Created August 4, 2012 05:09
Find and Replace urls in plain text with Perl(Schemeless)
use URI::Find;
sub replaceURL {
my $data = shift @_;
require URI::Find::userContentFile;
my $finder = URI::Find::Schemeless->new(
sub {
my($uri, $orig_uri) = @_;
return qq|<a href="$uri">$orig_uri</a>|;
}
);
@onevcat
onevcat / isValidateEmail.m
Created August 28, 2012 11:22
Email Validate
-(BOOL)isValidateEmail:(NSString *)email {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
@onevcat
onevcat / gist:3754865
Created September 20, 2012 09:19
define block
// Defining a block variable
BOOL (^isInputEven)(int) = ^(int input)
{
if (input % 2 == 0)
return YES;
else
return NO;
};
@onevcat
onevcat / gist:3754879
Created September 20, 2012 09:23
call a block
// Call similar to a C function call
int x = -101;
NSLog(@"%d %@ number", x, isInputEven(x) ? @"is an even" : @"is not an even");
@onevcat
onevcat / gist:3754886
Created September 20, 2012 09:24
block outer var use
float price = 1.99;
float (^finalPrice)(int) = ^(int quantity)
{
// Notice local variable price is
// accessible in the block
return quantity * price;
};
int orderQuantity = 10;
@onevcat
onevcat / gist:3754892
Created September 20, 2012 09:26
block outer var use 2
price = .99;
NSLog(@"Ordering %d units, final price is: $%2.2f", orderQuantity, finalPrice(orderQuantity));