Skip to content

Instantly share code, notes, and snippets.

@PaulTaykalo
Last active August 29, 2015 13:57
Show Gist options
  • Save PaulTaykalo/9829954 to your computer and use it in GitHub Desktop.
Save PaulTaykalo/9829954 to your computer and use it in GitHub Desktop.
What's the problem with this code?

Method

- (id)initFromKey:(NSString*)key {
    NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Library/Caches/%@", key]];

    @try
    {
        self = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    }
    @catch (NSException * e)
    {
        [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
        return nil;
    }

    return self;
}

Usage

MyObject * object = [[MyObject alloc] initFromKey:@"TheKey"];
if (!object) {
   object = [MyObject new];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment