Hi i have a function like this
- (void)save{
NSLog(@" %@ %@ %@ %@",AppAddressLine,AppCustomerName,AppPhoneNumber,AppPriceTier);
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
path = [documentsDirectory stringByAppendingPathComponent:@"NewInAppCustomer.plist"];
NSMutableArray *MainRoot=[[NSMutableArray alloc]initWithContentsOfFile:path];
NSMutableDictionary *ContentDictionary=[[NSMutableDictionary alloc]init];
[ContentDictionary setValue:AppCustomerName forKey:@"CustomerName"];
[ContentDictionary setValue:AppAddressLine forKey:@"CustomerAddress"];
[ContentDictionary setValue:AppPhoneNumber forKey:@"CustomerPhoneNumber"];
[ContentDictionary setValue:AppPriceTier forKey:@"CustomerPriceTier"];
[MainRoot addObject:ContentDictionary];
[MainRoot writeToFile:path atomically:YES];
NSLog(@"%@",MainRoot);
}
when I print with
NSLog(@" %@ %@ %@ %@",AppAddressLine,AppCustomerName,AppPhoneNumber,AppPriceTier);
it is showing a correct value,
but this line
NSLog(@"%@",MainRoot);
displays nil
as its value.
Can anyone please explain it to me?
This line:
NSMutableArray *MainRoot=[[NSMutableArray alloc]initWithContentsOfFile:path];
will return nil
if:
the file can’t be opened or the contents of the file can’t be parsed into an array
so you have a non-existent or an invalid file.
So, you should ensure that the file is created before this code runs, or, better, check the result and create a new empty array if you need to.
if (MainRoot == nil) MainRoot = [NSMutableArray array];