Problem parsing the .plist file

I have one plist file and I want to parse it and copy it to NSArray and the code I am using to do this.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

NSString *documentsPath = [paths objectAtIndex:0];

NSString *fooPath = [documentsPath stringByAppendingPathComponent:@"myfirstplist.plist"];
NSLog(fooPath);
self.myArray = [[NSArray arrayWithContentsOfFile:fooPath] retain];
NSLog(@"%@",myArray);

      

Now the problem is very strange, when I print the contents of myArray, it prints the file data, and sometime it doesn't.

I faced the same problem even when I use url as my path.

self.myArray = [[NSArray arrayWithContentsOfURL:URlPath] retain];

      

what is the reason?

Thanks in advance.

+1


source to share


5 answers


Depending on how you originally generated the .plist, you may run into problems if you try to read it as an array. The safest way to read plist is to use the NSPropertyListSerialization class: Apple Doc.



+4


source


To use this path,

NSString *plistPath = [bundle pathForResource:  @"file-name" ofType:@"plist"];

      



And then use it

NSArray *phrase2 = [NSArray arrayWithContentsOfFile: plistPath]; 
NSLog (@"%@", phrase2); 

      

+1


source


NSBundle *bundle = [NSBundle mainBundle]; 

NSString *plistPath = [bundle pathForResource:  @"file-name" ofType:@"plist"]; 

NSArray *phrase2 = [NSArray arrayWithContentsOfFile: plistPath];

NSLog (@"%@", phrase2); 

      

+1


source


Are you creating a writeToFile: atomically : file ? are you checking that this returns true?

0


source


It was a very stupid mistake,
I was declaring properties "myarray" as "persistent and non-atomic" and during the parsing operation I save it again,

self.myArray = [[NSArray arrayWithContentsOfURL:URlPath] retain];

      

means I saved it but never released it. That's why this weird problem was there.

Greetings.

0


source







All Articles