Reputation: 1721
I've a Plist File composed like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>1</key>
<dict>
<key>2</key>
<array>
<array>
<string>a</string>
<integer>1</integer>
</array>
<array>
<string>b</string>
<integer>0</integer>
</array>
<array>
<string>c</string>
<string>0</string>
</array>
</array>
</dict>
</dict>
</plist>
I try to read this file but I don't understand where is the error:
NSError *error;
NSMutableArray *result=[[NSMutableArray alloc] initWithCapacity:1];
NSString *pList = @"a.plist"; //
NSString *plistPath;
//path
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:pList];
//check if the path exist
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:plistPath];
if (!success) {
NSString *path = [[NSBundle mainBundle] pathForResource:pList ofType:@""];
[fileManager copyItemAtPath:path toPath:plistPath error:&error];
NSLog(@"error");
}
//read the dictionary
NSDictionary *temp = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
//get value for key of the dictionary
result=[temp valueForKey:@"1"];
NSMutableArray *tmp = [result objectAtIndex:1]; //this give me error
NSLog(@"n %d", [tmp count]);
...
if(!temp) [temp release];
Upvotes: 0
Views: 638
Reputation: 44633
In the root dictionary, the key 1
points to a dictionary. You are trying to assign it to result
which is an NSMutableArray
variable. So you are getting the error. Also, you are creating allocating an instance to result
and then reassigning it. This is a memory leak. The array you will be getting from the plist will be an immutable one. You will have to create a mutable copy if you intend to make changes to it.
Upvotes: 1