Re: Replacing values in NSDictionary
Re: Replacing values in NSDictionary
- Subject: Re: Replacing values in NSDictionary
- From: Sean Murphy <email@hidden>
- Date: Sun, 7 Jan 2007 12:38:38 -0500
On Jan 7, 2007, at 4:22 AM, Carl Johan Foss wrote:
I have an NSMutableArray containing NSDictionary objects in
"lFullResult", Now I would like to replace a value for the key "id"
whenever num = st.
How can I do that? the [lFullResult replaceValueAtIndex:0
inPropertyWithKey:@"id" withValue:vx]; does not seem to work, or I
do not know how to use it?
-(void)refreshLec:(NSNumber *)num
{
NSEnumerator *e = [lFullResult objectEnumerator];
id item;
while (item = [e nextObject]) {
NSNumber *st = [item valueForKey:@"id"];
if(![st isNSNull]) {
if([st isEqualToNumber:num]) {
MCPNumber *vx = [MCPNumber numberWithInt:1];
[lFullResult replaceValueAtIndex:0 inPropertyWithKey:@"id"
withValue:vx];
[lectureTable reloadData];
NSLog(@"Value = %@ and Key %@", st, item );
}
else {
//NSLog(@"No match!");
}
}
}
}
Hi Carl,
One possible way is to change around the code like this:
-(void)refreshLec:(NSNumber *)num
{
NSEnumerator *e = [lFullResult objectEnumerator];
id item;
while ((item = [e nextObject])) {
NSNumber *st = [item valueForKey:@"id"];
if ([st respondsToSelector:@selector(isEqualToNumber:)]) {
if([st isEqualToNumber:num]) {
[item setObject:[MCPNumber numberWithInt:1] forKey:@"id"];
NSLog(@"Value = %@ and Key %@", st, item );
}
}
}
}
Note: You'd probably have to use NSMutableDictionary objects instead,
to allow for changes. The documentation for NSMutableDictionary's
setObject:forKey: method explains "If aKey already exists in the
receiver, the receiver’s previous value object for that key is sent a
release message and anObject takes its place."
In addition to just getting the required behavior out of this method,
I'd also suggest taking a couple more steps to improve the
readability of the code, as it will make you a better engineer and
help you produce better software. It was kind of difficult to
examine this method without knowing anything about what your software
is doing here, but that doesn't have to be the case. If variable and
method names are explicit, someone on a mailing list only glancing at
this one single method can easily have a good grasp on what its job
is. Plus, when you come back to this area of your program a month
(or year) later, you won't necessarily know what its purpose is any
better than I did.
Maybe consider renaming the following symbols:
refreshLec: to refreshLecture:
num to lectureNumber
item to lecture
st to student
(lectures and students are only guesses, though, so even if those
symbols refer to other entities you get the idea.)
I also avoid using "magic numbers" in code, which is what that 1 is
here. It's difficult to tell what that 1 means. You could improve
readability by using a constant, such as DEFAULT_LECTURE or any other
name with explicitly indicates what the number 1 represents. This
also allows for simple changes in the future, especially when that
number is scattered throughout the code. The key @"id" could also
become a constant.
Sorry I gave more information than you were looking for :). Most of
your time spent engineering software will probably consist of reading
code. These steps to enhance readability and write self-documenting
code are worth it. Naming variables clearly and using constants are
a great (and simple) start.
Some great references:
ADC Cocoa Coding Guidelines <http://developer.apple.com/documentation/
Cocoa/Conceptual/CodingGuidelines/index.html>
Scott Stevenson's Cocoa Style for Objective-C <http://
cocoadevcentral.com/articles/000082.php>
Take care,
-Sean_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden