For the seasoned Cocoa developer,this is a pIEce of cake. For newer developers,this can be a real pain,especially if you don't kNow what you're looking for. I get this question a decent amount,so I figured I'd put a quick guIDe together.
The ProblemYou can't put just any object in a plist. This mainly gets people when they want to put something into NSUserDefaults and get an error (because NSUserDefaults archives to a pList under the hood).
PLists only support the core types: Nsstring
, NSNumber
,monospace">NSDate,monospace">NSData,monospace">NSArray,NSDictionary
(and their CF buddies thanks to the toll-free brIDge). The key here isNSData
. You can convert any object to NSData
with the NSCoding
protocol.
There are two things you have to do: implement NSCoding and then use the archiver and unarchiver.
Implementing NSCodingSay you have an object that looks like this:
@interface Note : NSObject { Nsstring *Title; Nsstring *author; BOol published;}@property (nonatomic, copy) Nsstring *Title;@property (nonatomic, copy) Nsstring *author;@property (nonatomic) BOol published;@end@H_301_133@#import "Note.h"@implementation Note@synthesize Title;@synthesize author;@synthesize published;- (voID)dealloc { [Title release]; [author release]; [super dealloc];}@end@H_301_133@Pretty simple,right?
Now,all you have to do to implement NSCoding is the following:
NSObject <NSCoding> { Nsstring *Title; Nsstring *author; dealloc { [Title release]; [author release]; [super dealloc];}- (ID)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { self.Title = [decoder decodeObjectForKey:@"Title"]; self.author = [decoder decodeObjectForKey:@"author"]; self.published = [decoder decodeBoolForKey:@"published"]; } return self;}- (encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:Title forKey:@"time"]; [encoder encodeObject:author forKey:@"author"]; [encoder encodeBool:published forKey:@"published"];}@end@H_301_133@Pretty simple. All I dID was add the
<NSCoding>
protocol delectation in the header file andinitWithCoder:
andencodeWithCoder:
in the implementation. You use these methods to tellNSCoder
how to encode your object into data. Notice how two of the variables are objects and one was aBOol
. You have to use different methods for non-objects. The NSCoder documentation has the full List.Remember,that you can use
Using the Archiver and UnarchiverNSCoder
to archive your object however whatever you want. It doesn't have to just be all of the instance variables in your object,although that's what you'll do 90% of the time.This part is also really easy. Let's say you have an array of notes that you want to put into
NSUserDefaults
,here's the code:// Given `notes` contains an array of Note objectsNSData *data = [NSKeyedArchiver archivedDataWithRootObject:notes];[[NSUserDefaults standardUserDefaults] setobject:data forKey:@"notes"];@H_301_133@Unarchiving is just as easy:
NSData *notesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"notes"];NSArray *notes = [NSKeyedUnarchiver unarchiveObjectWithData:notesData];@H_301_133@ 总结以上是内存溢出为你收集整理的Archiving Objective-C Objects with NSCoding全部内容,希望文章能够帮你解决Archiving Objective-C Objects with NSCoding所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)