Вопрос

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

Here is my ViewController.h

@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

Here is view Controller.m

#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

Here is my class Student.h

@interface MIGStudent : NSObject <NSCoding>

@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

And here is Student.m

#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

And here is what i get output every time I think everythings is working properly but there is some problem while retrieving the data

enter image description here

Thanks in advance

Это было полезно?

Решение

That's not a problem with your implementation of NSCoding, that's actually not even a problem at all! That's the way an NSArray prints itself literally (the result of the -description method). The fact that it does this indicates that the archiving/de-archiving process went smoothly. The array is the set of parenthesis (), and the objects within are in the format <Class : memory address>. If you had an array of invalid objects, then your array would most likely refuse to print, or crash when the students objects were added to it.

You can take advantage of this in your MIGStudent class and override -description to print a friendlier format. For example:

-(NSString*)description {
    return [NSString stringWithFormat:@"<%@ : %p Student named: %@ - who lives at: %@ With the ID number: %d and the phone number: %d>", NSStringFromClass(self.class), self, self.name, self.address, self.studentID, self.phoneNumber];
}

As a sidenote, initializers always call through to super. Your initWithCoder: method will always return nil.

Другие советы

Your initWithCoder: method is incorrect it will fail as currently it will return nil or some garbage value, it should be:

-(id) initWithCoder:(NSCoder *)aDecoder
{
     self = [super init];
     if (self)
     {
         //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;
 }

Secondly you can either override description or debugDescription method of your class to return custom information about that class.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top