• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Replacing values in NSDictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Replacing values in NSDictionary


  • Subject: Re: Replacing values in NSDictionary
  • From: Carl Johan Foss <email@hidden>
  • Date: Sun, 7 Jan 2007 22:25:18 +0100

Hi Sean,

Really appreciate the time you took to respond to my question. Obviously my Dictionary is an NSMutableDictionary but I made a mistake in my mail. Also thanks for the advice on the naming. You know sometimes I am too eager to write and experiment and then this gets a bit messy :-) But this is something I know I need to improve. For the number 1. I put that for my check box in a NSTableView to be ticket.

Thanks again.

Carl
On Jan 7, 2007, at 6:38 PM, Sean Murphy wrote:


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


References: 
 >Replacing values in NSDictionary (From: Carl Johan Foss <email@hidden>)
 >Re: Replacing values in NSDictionary (From: Sean Murphy <email@hidden>)

  • Prev by Date: Re: Figuring out if we have an active gateway
  • Next by Date: Re: Changing process uid
  • Previous by thread: Re: Replacing values in NSDictionary
  • Next by thread: Re: Replacing values in NSDictionary
  • Index(es):
    • Date
    • Thread