• 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: Scope of [NSMutableDictionary setObject:] in an array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scope of [NSMutableDictionary setObject:] in an array


  • Subject: Re: Scope of [NSMutableDictionary setObject:] in an array
  • From: Chris Giordano <email@hidden>
  • Date: Fri, 13 Sep 2002 15:12:27 -0400

Jeremy,

Hopefully this will make a little sense at least (it has been a really long week). Essentially, the difference here is akin to passing a parameter to a function by value or by reference. In the first case you show, you are copying the strings you want into new NSString objects, and inserting those into the dictionary. In the second case, you are passing in the strings from the textView objects directly. What is, I'm sure, happening is that in the second case, you are handing the dictionary a pointer to the textView string object. However, if you later change the textView string contents -- not the pointer mind you, but contents of what the pointer is pointing at -- that "new value" will also show up in the dictionary. In the first case, you are passing the value of the object to the dictionary, so to speak. In the second, you are passing the object itself, by reference, so if the object changes, those changes are also reflected in the dictionary.

Hopefully one of these two examples will help clarify in case the description above wasn't so clear.

A C example:

::: test.c :::

void doSomethingWithX(int val) { val = 5; return; }
/* by value -- value is copied in, x from main is unchanged */

void doSomethingToX(int * val) { (*val) = 5; return; }
/* by reference -- address of x is passed in, we are changing x directly */

int main()
{
int x = 10;
printf("Initially, x = %d\n", x);
doSomethingWithX(x);
printf("After doSomethingWithX(x), x = %d\n", x);
doSomethingToX(&x);
printf("After doSomethingToX(&x), x = %d\n", x);
return 0;
}

->cc test.c && ./a.out
Initially, x = 10
After doSomethingWithX(x), x = 10
After doSomethingToX(&x), x = 5


And then some bad ascii graphics:

Case 1:

+--------------+
| values |
| - Files List |--->[Copy of string1] <---(copy)---[textView1 string]
| - Parts Lits |--->[Copy of string2] <---(copy)---[textView2 string]
+--------------+

Now change [textView1 string] and [textView2 string] all you want, but those values won't be reflected anywhere but in textView1 and textView2.

Case 2:

+--------------+
| values |
| - Files List |--->[textView1 string]
| - Parts Lits |--->[textView2 string]
+--------------+

Because the strings are, themselves, in your dictionary, any changes will be reflected in your dictionary.


Hope this helps.

chris


On Friday, September 13, 2002, at 01:51 PM, Jeremy Dronfield wrote:

Could someone explain to me why it is that, whereas the following code works just fine...

- (IBAction)saveProject:(id)sender
{
NSMutableDictionary *values = [NSMutableDictionary dictionary];
NSString *listString = [NSString stringWithString:[textView1 string]];
NSString *partsListString = [NSString stringWithString:[textView2 string]];

[values setObject:[textField stringValue] forKey:@"Group"];
[values setObject:listString forKey:@"Files List"];
[values setObject:partsListString forKey:@"Parts List"];

[projects replaceObjectAtIndex:projectCounter withObject:values];

[prefs setObject:projects forKey:@"Projects"];
}

...the following version results in the current values of the text view strings replacing ALL the @"Files List" and @"Parts List" values in ALL the dictionaries in "projects"?

- (IBAction)saveProject:(id)sender
{
NSMutableDictionary *values = [NSMutableDictionary dictionary];

[values setObject:[groupField stringValue] forKey:@"Group"];
[values setObject:[textView1 string] forKey:@"Files List"];
[values setObject:[textView2 string] forKey:@"Parts List"];

[projects replaceObjectAtIndex:projectCounter withObject:values];

[prefs setObject:projects forKey:@"Projects"];
}

-Jeremy

========================================
email@hidden // email@hidden
The Alchemy Pages:
- fractious fiction at http://freespace.virgin.net/jeremy.dronfield
========================================
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Scope of [NSMutableDictionary setObject:] in an array
      • From: Jeremy Dronfield <email@hidden>
References: 
 >Scope of [NSMutableDictionary setObject:] in an array (From: Jeremy Dronfield <email@hidden>)

  • Prev by Date: Re: Bilingual shenanigans
  • Next by Date: Re: Bilingual shenanigans
  • Previous by thread: Scope of [NSMutableDictionary setObject:] in an array
  • Next by thread: Re: Scope of [NSMutableDictionary setObject:] in an array
  • Index(es):
    • Date
    • Thread