Skip to content

Instantly share code, notes, and snippets.

View hlxwell's full-sized avatar
⛷️
Focusing

Michael He hlxwell

⛷️
Focusing
View GitHub Profile
@hlxwell
hlxwell / new_gist_file
Created May 2, 2013 03:54
Simple popup in Mac
NSRunAlertPanel(@"Tracker already running", @"The Tracker is already running.", @"OK", nil, nil);
@hlxwell
hlxwell / new_gist_file
Created May 2, 2013 03:22
Throw exception
@throw [NSException exceptionWithName:@"GAJSException"
reason:@"Failed to load JavaScriptEngine"
userInfo:nil];
@hlxwell
hlxwell / new_gist_file
Created May 2, 2013 03:16
Define a static method, for escape string
static NSString* GAEscapeNSString(NSString* value) {
if (!value) return nil;
const char *chars = [value UTF8String];
NSMutableString *escapedString = [NSMutableString string];
while (*chars) {
if (*chars == '\\') {
[escapedString appendString:@"\\\\"];
} else if (*chars == '\'') {
[escapedString appendString:@"\\'"];
} else {
@hlxwell
hlxwell / new_gist_file
Created May 2, 2013 03:06
Archive/Unarchive object to/from file
[NSKeyedArchiver archiveRootObject:Object toFile:filePath]
[NSKeyedUnarchiver unarchiveObjectWithFile:filePath]
@hlxwell
hlxwell / new_gist_file
Created May 2, 2013 02:54
Mutex in objective-c
@synchronized(self) {
// Code here will be execute by only one thread.
}
@hlxwell
hlxwell / new_gist_file
Created April 23, 2013 05:58
Calculate the age
NSDate* now = [NSDate date];
NSDateComponents* ageComponents = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit
fromDate:self.birthday
toDate:now
options:0];
NSInteger age = [ageComponents year];
@hlxwell
hlxwell / new_gist_file
Created April 19, 2013 05:13
Command lines to ignore xcuserstate file
git rm --cached ProjectFolder.xcodeproj/project.xcworkspace/xcuserdata/myUserName.xcuserdatad/UserInterfaceState.xcuserstate
git commit -m "Removed file that shouldn't be tracked"
*.app
build/
.DS_Store
*~
.LSOverride
*.xcuserdatad
*.xcworkspace
@hlxwell
hlxwell / new_gist_file
Created April 17, 2013 06:38
NSObject method for sending message to them.
NSObject
Sending Messages
– performSelector:withObject:afterDelay:
– performSelector:withObject:afterDelay:inModes:
– performSelectorOnMainThread:withObject:waitUntilDone:
– performSelectorOnMainThread:withObject:waitUntilDone:modes:
– performSelector:onThread:withObject:waitUntilDone:
– performSelector:onThread:withObject:waitUntilDone:modes:
– performSelectorInBackground:withObject:
+ cancelPreviousPerformRequestsWithTarget:
@hlxwell
hlxwell / new_gist_file
Created April 17, 2013 05:12
Cleanup Icon Badge Number
- (void)applicationDidBecomeActive:(UIApplication *)application
{
application.applicationIconBadgeNumber = 0;
}