title: Archiving and retrieving array from a class in Objective-c
body: <p>Hi i am trying to save data in a file with the help of a class that is saved to disk. It is saving the data but not retrieving correct data. Plz help me</p>
  
  <p>Here is my ViewController.h</p>
  
  <pre><code>@interface MIGViewController : UIViewController
  - (IBAction)hideKeyboard:(id)sender;
  @property (strong, nonatomic) NSMutableArray * myArray;
  @property (strong, nonatomic) NSString * pathToFile;
  @property (weak, nonatomic) IBOutlet UITextField *nameField;
  @property (weak, nonatomic) IBOutlet UITextField *idField;
  @property (weak, nonatomic) IBOutlet UITextField *addressField;
  @property (weak, nonatomic) IBOutlet UITextField *phoneField;
  - (IBAction)addButtonTapped:(id)sender;
  - (IBAction)saveButtonTapped:(id)sender;
  
  @end
  </code></pre>
  
  <p>Here is view Controller.m</p>
  
  <pre><code>#import "MIGViewController.h"
  #import "MIGStudent.h"
  
  @interface MIGViewController ()
  
  @end
  
  @implementation MIGViewController
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString * documentsPath = [paths objectAtIndex:0];
      self.pathToFile = [documentsPath stringByAppendingPathComponent:@"students.sukh"];
  
      NSFileManager * manager = [NSFileManager defaultManager];
      if ([manager fileExistsAtPath:self.pathToFile])
      {
          //File exists
          self.myArray = [NSKeyedUnarchiver unarchiveObjectWithFile:self.pathToFile];
  
          UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Students array" message:[self.myArray description]delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
          [alert show];
      }
      else
      {
          //File doesn't exist
          self.myArray = [NSMutableArray array];
      }
  }
  
  - (void)didReceiveMemoryWarning
  {
      [super didReceiveMemoryWarning];
      // Dispose of any resources that can be recreated.
  }
  
  - (IBAction)addButtonTapped:(id)sender {
      NSString * name =self.nameField.text;
      NSString * address = self.addressField.text;
      int studentID = [self.idField.text intValue];
      int phoneNumber = [self.phoneField.text intValue];
      MIGStudent * student = [[MIGStudent alloc] initWithName:name address:address studentID:studentID phoneNumber:phoneNumber];
      [self.myArray addObject:student];
      self.nameField.text = @"";
      self.addressField.text = @"";
      self.idField.text = @"";
      self.phoneField.text = @"";
  }
  
  - (IBAction)saveButtonTapped:(id)sender {
      [NSKeyedArchiver archiveRootObject:self.myArray toFile:self.pathToFile];
  }
  - (IBAction)hideKeyboard:(id)sender {
      [self resignFirstResponder];
  }
  @end
  </code></pre>
  
  <p>Here is my class Student.h</p>
  
  <pre><code>@interface MIGStudent : NSObject &lt;NSCoding&gt;
  
  @property (strong, nonatomic) NSString * name;
  @property (strong, nonatomic) NSString * address;
  @property (nonatomic) int studentID;
  @property (nonatomic) int phoneNumber;
  
  -(id)initWithName:(NSString *) name address:(NSString *)address studentID: (int)studentID phoneNumber:(int) phoneNumber;
  
  @end
  </code></pre>
  
  <p>And here is Student.m</p>
  
  <pre><code>#import "MIGStudent.h"
  
  @implementation MIGStudent
  
  -(id) initWithName:(NSString *)name address:(NSString *)address studentID:(int)studentID phoneNumber:(int)phoneNumber
  {
      if (self=[super init])
      {
          self.name = name;
          self.address = address;
          self.phoneNumber = phoneNumber;
          self.studentID = studentID;
      }
      return self;
  }
  
  -(void) encodeWithCoder:(NSCoder *)aCoder
  {
     //Used when saving to disk
      [aCoder encodeObject:self.name forKey:@"name"];
      [aCoder encodeObject:self.address forKey:@"address"];
      [aCoder encodeInt:self.phoneNumber forKey:@"phoneNumber"];
      [aCoder encodeInt:self.studentID forKey:@"studentID"];
  }
  
  -(id) initWithCoder:(NSCoder *)aDecoder
  {
      //Used when reading from disk
      self.name = [aDecoder decodeObjectForKey:@"name"];
      self.address = [aDecoder decodeObjectForKey:@"address"];
      self.phoneNumber = [aDecoder decodeIntForKey:@"phoneNumber"];
      self.studentID = [aDecoder decodeIntForKey:@"studentID"];
  
      return self;
  }
  @end
  </code></pre>
  
  <p>And here is what i get output every time
  I think everythings is working properly but there is some problem while retrieving the data</p>
  
  <p><img src="https://i.stack.imgur.com/FFIyp.png" alt="enter image description here"></p>
  
  <p>Thanks in advance</p>