tags:

views:

1035

answers:

5

Hi,

I've one plist file and I want to parse it and copy it's content into NSArray,and code that I am using for that is.

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 problem is very weird, sometime when I print myArray content it prints file data, and sometime it doesn't.

I am facing a same problem even when I use URL as my path.

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

what would be the reason?

Thanks in advance.

A: 

Are you generating the file with writeToFile:atomically: ? do you check that this returns true?

superfell
+4  A: 

Depending on how you generated the .plist initially, you may run into problems if you try and read it back in as an array. The safest way to read a plist is using the NSPropertyListSerialization class: Apple Doc.

Ben Gottlieb
A: 

It was very stupid mistake,
I declared "myarray" properties as "retain and nonatomic" and during parsing operation I am retaining it again,

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

means I retained it but never released it.that's why that weird problem was there.

Cheers.

Amit Vaghela
A: 

//to get the pat use

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

//and then use it

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

alex davila
A: 

//sorry

NSBundle *bundle = [NSBundle mainBundle];

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

NSArray *phrase2 = [NSArray arrayWithContentsOfFile: plistPath];

NSLog (@"%@", phrase2);

alex davila