Created
July 4, 2011 17:39
-
-
Save slashingweapon/1063677 to your computer and use it in GitHub Desktop.
Useful Objective-C Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a path to a directory, and make sure it exists. | |
// The iOS documentation will tell you that URLs are preferred, but there is no createDirectory method for | |
// URLs. So we have to use string paths instead. | |
// Create a sub-directory in the application's Documents directory. Return the path on success, or nil on failure. | |
- (void)createDocumentSubdirectory:(NSString*)dirName { | |
NSString *retval = nil; | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *dirPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:dirName]; | |
NSFileManager *fm = [[NSFileManager alloc] init]; | |
if (dirPath && fm) { | |
if ([fm createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil]) | |
retval = dirPath; | |
} | |
if (fm) | |
[fm release]; | |
return retval; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A very simple predicate example, that allows you to remove items from an array that match some criteria | |
// In this case, we want to remove everything with a given uuid | |
NSPredicate *pred = [NSPredicate predicateWithFormat:@"uuid == %@", uuid]; | |
NSArray *removableThings = [self.targetArray filteredArrayUsingPredicate:pred]; | |
[self.targetArray removeObjectsInArray:removableThings]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is borrowed from my character sheet program. | |
// Assume we have a FateCharacter class... | |
id character = [NSKeyedUnarchiver unarchiveObjectWithFile:oneFullPath]; | |
if ([character isKindOfClass:[FateCharacter class]]) | |
[self.characterArray addObject:character]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment