Archiving Objective-C Objects with NSCoding

Archiving Objective-C Objects with NSCoding,第1张

概述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

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 Problem

You 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: NsstringNSNumber,monospace">NSDate,monospace">NSData,monospace">NSArray,NSDictionary (and their CF buddies thanks to the toll-free brIDge). The key here isNSDataYou can convert any object to NSData with the NSCoding protocol.

The Solution

There are two things you have to do: implement NSCoding and then use the archiver and unarchiver.

Implementing NSCoding

Say 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 and initWithCoder: and encodeWithCoder: in the implementation. You use these methods to tell NSCoder how to encode your object into data. Notice how two of the variables are objects and one was a BOol. You have to use different methods for non-objects. The NSCoder documentation has the full List.

Remember,that you can use NSCoder 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.

Using the Archiver and Unarchiver

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所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1059819.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存