Created
March 11, 2012 19:01
-
-
Save subdigital/2017689 to your computer and use it in GitHub Desktop.
Property List Serialization
This file contains hidden or 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
// Bookmark.h | |
@interface Bookmark : NSObject <NSCoding> | |
@property (nonatomic, copy) NSString *label; | |
@property (nonatomic, copy) NSString *url; | |
@end | |
// Bookmark.m | |
#import "Bookmark.h" | |
@implementation Bookmark | |
@synthesize label = _label; | |
@synthesize url = _url; | |
- (id)initWithCoder:(NSCoder *)aDecoder { | |
self = [super init]; | |
if (self) { | |
self.label = [aDecoder decodeObjectForKey:@"label"]; | |
self.url = [aDecoder decodeObjectForKey:@"url"]; | |
} | |
return self; | |
} | |
- (void)encodeWithCoder:(NSCoder *)aCoder { | |
[aCoder encodeObject:self.label forKey:@"label"]; | |
[aCoder encodeObject:self.url forKey:@"url"]; | |
} | |
- (void)dealloc { | |
[_label release]; | |
[_url release]; | |
[super dealloc]; | |
} | |
@end | |
// archiving the object | |
[NSKeyedArchiver archiveRootObject:_bookmarks toFile:_path]; | |
// restoring the object | |
_bookmarks = [[NSKeyedUnarchiver unarchiveObjectWithFile:_path] retain]; |
This file contains hidden or 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
// writing to a file | |
NSArray *flavors = [NSArray arrayWithObjects:@"chocolate", @"vanilla", | |
@"strawberry", nil]; | |
[flavors writeToFile:PATH atomically:YES]; | |
// reading from a file | |
NSArray *flavorsFromDisk = [NSArray arrayWithContentsOfFile:PATH]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment