title: read and write using NSKeyedArchiver, ios
body: <p>Below is a class to read and write data using nsarchive</p>
  
  <p><strong>Data.m</strong></p>
  
  <pre><code>-(id)init {
      self        =   [super init];
      if(self) {
          arr     =   [[NSMutableArray alloc] init];
      }
      return self;
  }
  
  -(NSString *)getPath {
      NSArray     *paths          =   NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString    *documentPath;
  
      if ([paths count] &gt; 0)
          documentPath            =   [paths objectAtIndex:0];
  
      NSString *draftDataPath     =   [documentPath stringByAppendingPathComponent:@"draftData.dat"];
      return draftDataPath;
  }
  
  -(void)saveDataToDisk {
      NSString *path              =   [self getPath];
      [NSKeyedArchiver archiveRootObject:arr toFile:path];
  
  }
  
  -(void)loadDataFromDisk {
      NSString    *path           =   [self getPath];
      self.arr                    =   [NSKeyedUnarchiver unarchiveObjectWithFile:path];
  }
  </code></pre>
  
  <p>At later on, I am adding some objects into <code>arr</code> by doing
  CustomerClass.m</p>
  
  <pre><code>- (void) viewDidLoad {
  
  Data *data = [[Data alloc] init];
  [data.arr addObject:myObject1]
  [data.arr addObject:myObject2]
  [data.arr addObject:myObject3]
  [data saveDataToDisk];
  }
  </code></pre>
  
  <p>At DisplayData.m, I want to check data.arr by</p>
  
  <pre><code>- (void) viewDidLoad {
     Data *data = [[Data alloc] init];
     [data loadDataFromDisk];
  
     NSLog(@"length of array is %d",[data.arr count]);
  
  }
  </code></pre>
  
  <p>On the console, I am getting</p>
  
  <pre><code>length of array is 1
  </code></pre>
  
  <p>I thought it should be 3 after all. </p>
  
  <p>Please point out what I have just made a mistake in the middle of work if you have any clues about it.</p>