iphone - Problem reading data from a plist -


- (void)viewdidload {    [super viewdidload];      paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);      nsstring *documentsdirectory = [paths objectatindex:0];      path = [documentsdirectory stringbyappendingpathcomponent:@"plist.plist"];       nsfilemanager *filemanager = [nsfilemanager defaultmanager];      if (![filemanager fileexistsatpath: path])      {         path = [documentsdirectory stringbyappendingpathcomponent: [nsstring stringwithformat: @"plist.plist"] ];     }      nsmutabledictionary *data = [[nsmutabledictionary alloc] initwithcontentsoffile: path];     int value = 5;     [data setobject:[nsnumber numberwithint:value] forkey:@"value"];     [data writetofile: path atomically:yes];     [data release];      nsmutabledictionary *savedstock = [[nsmutabledictionary alloc] initwithcontentsoffile: path];     int value1;     value1 = [[savedstock objectforkey:@"value"] intvalue];     **nslog(@"%i",value1);**     [savedstock release];   } 

i have saved value in plist... want retrieve it. if print using nslog displays 0. how should retrieve value?

the problem

in code:

paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);  nsstring *documentsdirectory = [paths objectatindex:0];  path = [documentsdirectory stringbyappendingpathcomponent:@"plist.plist"];  

path contains file path rooted @ document directory. let’s it’s …/plist.plist.

nsfilemanager *filemanager = [nsfilemanager defaultmanager];  if (![filemanager fileexistsatpath: path])  {     path = [documentsdirectory stringbyappendingpathcomponent: [nsstring stringwithformat: @"plist.plist"] ]; } 

this odd. if file doesn’t exist, append /plist.plist path variable, becomes …/plist.plist/plist.plist, doesn’t exist. considering this,

nsmutabledictionary *data = [[nsmutabledictionary alloc] initwithcontentsoffile: path]; 

-initwithcontentsoffile: returns nil, data nil, so:

int value = 5; [data setobject:[nsnumber numberwithint:value] forkey:@"value"]; [data writetofile: path atomically:yes]; [data release]; 

doesn’t , in:

nsmutabledictionary *savedstock = [[nsmutabledictionary alloc] initwithcontentsoffile: path]; int value1; value1 = [[savedstock objectforkey:@"value"] intvalue]; 

savedstock nil, hence -objectforkey: followed -intvalue returns 0.

nslog(@"%i",value1); [savedstock release]; 

to validate assumption, use debugger or nslog() inspect contents of path, data, , savedstock.

one solution

if file doesn’t exist, cannot read it. hence:

nsfilemanager *filemanager = [nsfilemanager defaultmanager];  if (![filemanager fileexistsatpath: path])  {     path = [documentsdirectory stringbyappendingpathcomponent: [nsstring stringwithformat: @"plist.plist"] ]; }  nsmutabledictionary *data = [[nsmutabledictionary alloc] initwithcontentsoffile: path]; 

should be:

nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nsmutabledictionary *data;  if ([filemanager fileexistsatpath: path])  {     // if file exists, read dictionary file     data = [[nsmutabledictionary alloc] initwithcontentsoffile: path]; } else {     // if file doesn’t exist, create empty dictionary     data = [[nsmutabledictionary alloc] init]; } 

and:

int value = 5; [data setobject:[nsnumber numberwithint:value] forkey:@"value"]; [data writetofile: path atomically:yes]; [data release]; 

should create …/plist.plist containing dictionary. and:

nsmutabledictionary *savedstock = [[nsmutabledictionary alloc] initwithcontentsoffile: path]; int value1; value1 = [[savedstock objectforkey:@"value"] intvalue]; nslog(@"%i",value1); [savedstock release]; 

should work because corresponding file created in previous steps.


Comments