Reputation: 185
In my app I use the following code to set a value of an array to zero:
[[records objectAtIndex:prizenotify] setValue:nil forKey:@"marbles"];
NSError *error;
[_managedObjectContext save:&error]; //Save
But, It crashes my code. Am I coding this correctly? Marbles is an NSNumber.
Thanks!
Upvotes: 0
Views: 393
Reputation: 29552
[[records objectAtIndex:prizenotify] setValue:[NSNumber numberWithInteger:0] forKey:@"marbles"];
Or in modern Objective-C:
[records[prizenotify] setValue:@0 forKey:@"marbles"];
Upvotes: 2
Reputation: 23510
Make sure that [records objectAtIndex:prizenotify]
object is a NSMutableDictionary.
To be sure, test :
[[((NSDictionary*)[records objectAtIndex:prizenotify]) mutableCopy] setValue:[NSNumber numberWithInteger:0] forKey:@"marbles"];
if this one works, the problem is that you just have a NSDictionary (not mutable).
If you have a NSMutableDictionary for sure :
If you want to set the value to zero, call :
[[records objectAtIndex:prizenotify] setValue:[NSNumber numberWithInteger:0] forKey:@"marbles"];
If you want to delete the 'marbles' key from the dictionary, call :
[[records objectAtIndex:prizenotify] removeObjectForKey:@"marbles"];
Upvotes: 0
Reputation: 16827
Although I would choose Sean's solution ([NSNull null]
) as the simplest solution, there is also the NSPointerArray class (which specifically answers your question). Here's the overview:
NSPointerArray is a mutable collection modeled after NSArray but it can also hold NULL values, which can be inserted or extracted (and which contribute to the object’s count). Moreover, unlike traditional arrays, you can set the count of the array directly. In a garbage collected environment, if you specify a zeroing weak memory configuration, if an element is collected it is replaced by a NULL value.
Upvotes: 0
Reputation: 5820
If you're looking to set a nil-esque value, you can use [NSNull null]
to insert null/nil values into a collection class like NSArray.
Upvotes: 0