Skip to content

Instantly share code, notes, and snippets.

@slashingweapon
Created July 4, 2011 17:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save slashingweapon/1063677 to your computer and use it in GitHub Desktop.
Save slashingweapon/1063677 to your computer and use it in GitHub Desktop.
Useful Objective-C Snippets
// 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;
}
// 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 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