Reputation: 5084
I'm using a property-file with this structure:
maxRating Number --> 5
gradePercent Dictonary
>grade1 Number --> 0.85
>grade2 Number --> 0.70
>grade3 Number --> 0.55
>grade4 Number --> 0.40
To read the properties I'm using
NSDictionary *plist = [[NSDictionary alloc] initWithContentsOfFile:filepath];
rating = [[plist objectForKey:@"maxRating"] intValue];
gradePercent = (NSMutableDictionary*)[[plist objectForKey:@"gradePercent"] copy];
Till here every thing works perfectly...I can get the right objects with [gradePercent objectForKey:@"grade1"]
but when I try to set one of them with
[gradePercent setObject:[NSNumber numberWithFloat:0.90] forKey:@"grade1"]
I always get a SIGABRT error and the APP is crashing.
Does anyone see why this isn't working? Because I don't -.-
Upvotes: 0
Views: 2506
Reputation: 12714
[[plist objectForKey:@"gradePercent"] copy]
returns an immutable dictionary.
What you need is [[plist objectForKey:@"gradePercent"] mutableCopy]
Upvotes: 4
Reputation: 32681
Because the dictionary you have is read only to get a Mutable dictionary from any dictionary you need to call addEntriesFromDictionary on a Muntable dictionary
e.g.
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
[dict addEntriesFromDictionary:[plist objectForKey:@"gradePercent"]];
the [plist objectForKey:@"gradePercent"] is a NSDictionary and so cannot be added to. You cannot simply cast from NSDictionary* to NSMutableDictionary *.
Upvotes: 1
Reputation: 92335
An NSDictionary
is immutable, and simply casting the pointer to NSMutableDictionary *
doesn't make the object magically become mutable. So you need a mutable dictionary. You could do it like this:
NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:filepath];
Upvotes: 4