现在的位置: 首页 > 综合 > 正文

NSKeyedArchiver

2013年03月12日 ⁄ 综合 ⁄ 共 2679字 ⁄ 字号 评论关闭

The interface we define here is called Account:

   1  #import <Foundation/Foundation.h>
   2  
   3  @interface Account : NSObject <NSCoding> {
   4      NSString * username;
   5  }
   6  
   7  @property (retain) NSString * username;
   8  
   9  -(NSString *)description;
  10  
  11  @end

The implementation is this:

   1  #import "Account.h"
   2  
   3  @implementation Account
   4  
   5  @synthesize username;
   6  
   7  // Called when unserialized
   8  - (id) initWithCoder: (NSCoder *)coder {
   9      if (self = [super init]) {
  10          self.username = [coder decodeObjectForKey:@"username"];
  11      }
  12      return self;
  13  }
  14  
  15  // Called when serialized
  16  - (void) encodeWithCoder: (NSCoder *)coder {
  17      [coder encodeObject: self.username forKey:@"username"];
  18  }
  19  
  20  -(NSString *)description {
  21  	return [NSString stringWithFormat:@"Account { username: '%@' }", self.username];
  22  }
  23  
  24  - (void) dealloc {
  25      [username release];
  26      [super dealloc];
  27  }
  28  @end

The application delegate interface named TheAppDelegate is defined here:

   1  @interface TheAppDelegate : NSObject <UIApplicationDelegate> {
   2      UIWindow *window;
   3      NSMutableDictionary * accounts;
   4      ...
   5  }
   6  
   7  @property (nonatomic, retain) NSMutableDictionary * accounts;
   8  
   9  
  10  - (NSString *) pathForDataFile;
  11  - (void) saveDataToDisk;
  12  - (void) loadDataFromDisk;

The application delegate implementation has these methods defined:

   1  // Called when application launches
   2  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   3      [self loadDataFromDisk]; 
   4  }
   5  
   6  // Path to the data file in the app's Documents directory
   7  - (NSString *) pathForDataFile {
   8      NSArray*	documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   9      NSString*	path = nil;
  10  	
  11      if (documentDir) {
  12          path = [documentDir objectAtIndex:0];    
  13      }
  14  	
  15      return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];
  16  }
  17  
  18  - (void) saveDataToDisk {
  19      NSString * path = [self pathForDataFile];
  20      NSLog(@"Writing accounts to '%@' %@", path, self.accounts);
  21  	
  22      NSMutableDictionary * rootObject;
  23      rootObject = [NSMutableDictionary dictionary];
  24      
  25      [rootObject setValue: [self accounts] forKey:@"accounts"];
  26      [NSKeyedArchiver archiveRootObject: rootObject toFile: path];
  27  }
  28  
  29  - (void) loadDataFromDisk {
  30      NSString     * path         = [self pathForDataFile];
  31      NSLog(@"Loading accounts from file '%@'", path);
  32  	
  33      NSDictionary * rootObject;
  34      
  35      rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
  36      self.accounts = [rootObject valueForKey:@"accounts"];
  37      NSLog(@"Loaded accounts %@", accounts);
  38      if (self.accounts == nil) {
  39          self.accounts = [[NSMutableDictionary alloc] init];  
  40       }
  41  }

Now to save the account data to data.bin in the app’s Documents folder you only have to call
[self saveDataToDisk], or if you’re doing it from a controller use this:

   1  TheAppDelegate *delegate = (TheAppDelegate *)[UIApplication sharedApplication].delegate;
   2  [delegate saveDataToDisk];

The code was adapted from
this article
.

 

http://snippets.aktagon.com/snippets/475-How-to-use-NSKeyedArchiver-to-store-user-settings-on-the-iPhone

抱歉!评论已关闭.