NSUserDefaults, object archiving, nsuserdefaults Archiving

Source: Internet
Author: User

NSUserDefaults, object archiving, nsuserdefaults Archiving

NSUserDefaults

@ Interface Person: NSObject <NSCoding>
@ Property (nonatomic, assign) NSInteger ID;
@ Property (nonatomic, copy) NSString * name;
@ End

 

// Save

NSUserDefaults * user = [NSUserDefaults standardUserDefaults];

[User setInteger: ID forKey: @ "ID"];
[User setObject: name forKey: @ "name"];

[User synchronize];

// Obtain

NSInteger uId = [[[NSUserDefaults standardUserDefaults] integerValueForKey: @ "ID"];
NSString * name = [[NSUserDefaults standardUserDefaults] stringForKey: @ "name"];

 

Access by Object

// Save
[User setObject: self forKey: @ "user"];

[User synchronize];

// Obtain
User * u = [UserObjectForKey "@" user "];

 

2. Object Archiving
To use Object archiving, the Object must implement the NSCoding protocol. Most Object C objects comply with the NSCoding protocol. You can also implement the NSCoding protocol in custom objects. To implement the NSCoding Protocol, two methods are implemented:
-(Void) encodeWithCoder :( NSCoder *) encoder;

-(Void) initWithCoder :( NSCoder *) encoder.
At the same time, it is recommended that the object also implement the NSCopying protocol, which allows copying objects. To implement the NSCopying protocol, the-(id) copyWithZone :( NSZone *) zone method must be implemented.
@ Interface Person: NSObject <NSCoding>
@ Property (nonatomic, assign) NSInteger ID;
@ Property (nonatomic, copy) NSString * name;
@ End

@ Implementation User
// The following two methods must be implemented, otherwise crash will be called
-(Void) encodeWithCoder :( NSCoder *) ACO;
{
// Put the attributes that need to be persisted here
[Ecoder encodeObject: [NSNumber numberWithInteger: self. ID] forKey: @ "ID"];
[Ecoder encodeObject: self. name forKey: @ "name"];
}
-(Id) initWithCoder :( NSCoder *) aDecoder
{
If (self = [self init])
{
// It must be consistent with the content in the encodeWithCoder method. Otherwise, no data can be read.
Self. userID = [[aDecoder decodeObjectForKey: @ "ID"] integerValue];
Self. name = [aDecoder decodeObjectForKey: @ "name"];
}
Return self;
}

// Usage
+ (BOOL) save {
NSError * error = nil;
// Determine the storage path, usually the file under the Document directory
NSString * fileName = [self getFileName];
NSString * filePath = [self getFilePath];
If (! [[NSFileManager defaultManager] createDirectoryAtPath: filePath withIntermediateDirectories: YES attributes: nil error: & error]) {
NSLog (@ "failed to create user file directory ");
Return NO;
}
Return [NSKeyedArchiver archiveRootObject: self toFile: [fileName: userId];
}

@ End

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.