Ahmed Alkaabi
Ahmed Alkaabi

Reputation: 61

Saving data into plist with iOS

I am trying to add data to plist but I could not, let's tell you what I am doing:

Have a look to my plist:

enter image description here

Lets see my code, I created 2 arrays:

@property NSMutableArray *nameArr;
@property NSMutableArray *countryArr;

Here is the code where I save the data:

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];

[self.nameArr addObject:self.theName.text];
[self.countryArr addObject:self.cellPhone.text];

NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.nameArr, self.countryArr, nil] forKeys:[NSArray arrayWithObjects: @"city", @"state", nil]];

NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

if(plistData)
{
    [plistData writeToFile:plistPath atomically:YES];
    NSLog(@"Data Saved");

}
else
{
    NSLog(@"Data not saved");

}

The below image shows the error, the app terminate but I do not know where is the problem.

enter image description here

Upvotes: 0

Views: 185

Answers (1)

vadian
vadian

Reputation: 285064

Probably the array properties are declared but never initialized.

You have to add

nameArr = [[NSMutableArray alloc] init];
countryArr = [[NSMutableArray alloc] init];

somewhere before using them.

Regarding the warning use the method suggested by the compiler.

Upvotes: 1

Related Questions