setObject ForKey
I am attempting to incorporate a plist into my project. I have accomplish the extraction of the 开发者_Python百科data from the plist to my project. Now I want to write back the modified data. For instance, I have a textField in which the data is stored as an integer. Now I want to capture that integer data and store it in an NSMutable Dictionary *temp using:
[temp setObject:<what goes here?> forKey:@"key"];
prior to writing it all out to the plist. All I end up getting is the pointer information and not the value of the textField into the dictionary. I am using the following to examine the dictionary:
for (id key in temp) {
NSLog(@"entry %@ value %@", key, [temp valueForKey:key]);
}
What you want is something like:
[temp setObject:[NSNumber numberWithInteger:[textField integerValue]] forKey:@"key"];
if you're coding for OS X.
Or something like:
[temp setObject:[NSNumber numberWithInteger:[textField.text integerValue]] forKey:@"key"];
if you're coding for iOS.
Just to make sure OP doesn't miss edc1591's comment: NSDictionary implements a method objectForKey:, which should be used in favor of the generic KVC method valueForKey:. See this answer for more info on objectForKey:
vs. valueForKey:
Edit: Overlooked OP mentioning the values being integers. Fixed answer. Edit: Added remark on valueForKey: usage.
精彩评论