NSUserDefaults for iPhone file operations: Reading and Writing custom objects

Source: Internet
Author: User

IPhoneFile OperationsNSUserDefaultsRead and WriteCustomThe object is the content to be introduced in this article.NSUserDefaultsRead and WriteCustomObject. Let's just look at the content first.NSUserDefaultsYou can access some short information, such as saving and reading a stringNSUserDefaults:

 
 
  1. NSString *string = [NSString stringWithString @"hahaha"];     
  2. NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];     
  3. [ud setObject:string forKey:@"myKey"];     
  4. NSString *value;     
  5. value = [ud objectForKey:"myKey"];   

But not all things can be put in. NSUserDefaults only supports: NSString, NSNumber, NSDate, NSArray, NSDictionary.

If a custom class is saved to an NSArray and then saved to NSUserDefaults, the operation fails. If you do not believe it, try it. If you have succeeded, please let me know.

What should we do?

The method I found is to make this custom class implement the-(id) initWithCoder: (NSCoder *) coder method and-(void) encodeWithCoder: (NSCoder *) in the <NSCoding> protocol *) coder method obj-c protocol is the java interface, is the pure virtual function of C ++), and then encode the custom Class Object To NSData, then read from NSUserDefaults.

Suppose there is such a simple Class Object

 
 
  1. @interface BusinessCard : NSObject <NSCoding>{     
  2.     NSString *_firstName;     
  3.     NSString *_lastName;     
  4. }     
  5. @property (nonatomic, retain) NSString *_firstName;     
  6. @property (nonatomic, retain) NSString *_lastName;     
  7. @end;     
  8.      
  9. @implementation BusinessCard     
  10. @synthesize _firstName, _lastName;     
  11. - (void)dealloc{     
  12.     [_firstName release];     
  13.     [_lastName release];     
  14.     [super dealloc];     
  15. }     
  16. - (id) initWithCoder: (NSCoder *)coder     
  17. {     
  18.     if (self = [super init])     
  19.     {     
  20.         self._firstName = [coder decodeObjectForKey:@"_firstName"];     
  21.         self._lastName = [coder decodeObjectForKey:@"_lastName"];     
  22.     }     
  23.     return self;     
  24. }     
  25. - (void) encodeWithCoder: (NSCoder *)coder     
  26. {     
  27.     [coder encodeObject:_firstName forKey:@"_firstName"];     
  28.     [coder encodeObject:_lastName forKey:@"_lastName"];        
  29. }     
  30. @end   

Then, NSData is used as the carrier during access:

 
 
  1. BusinessCard *bc = [[BusinessCard alloc] init];     
  2. NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];     
  3. NSData *udObject = [NSKeyedArchiver archivedDataWithRootObject:bc];     
  4. [ud setObject:udObject forKey:@"myBusinessCard"];     
  5. [bc release];     
  6. udObject = nil;     
  7. udObject = [ud objectForKey:@"myBusinessCard"];     
  8. bc = [NSKeyedUnarchiver unarchiveObjectWithData:udObject] ;  

The above code is intercepted by another program and has not been tested, but it means that. IfCustomFrom anotherCustomClass Object, then all Nested classes must be implemented <NSCoding>.

Summary:IPhoneFile OperationsNSUserDefaultsRead and WriteCustomThe object content has been introduced. I hope this article will help you!

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.