Amazon.com Widgets An answer to the problem in 18.3 but also a problem with changing a key?
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 22, 2014, 10:41:58 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 18
| | | |-+ An answer to the problem in 18.3 but also a problem with changing a key?
Pages: [1] Go Down
Print
Author Topic: An answer to the problem in 18.3 but also a problem with changing a key? (Read 1319 times)
MarkReid
Full Member
***
Posts: 173






on: May 09, 2010, 11:40:37 AM

Code: (Objective-C)
// Create an NSDictionary dictionary object and fill it with some key/object pairs. Then make both
// mutable and immutable copies. Are these deep copies or shallow copies that are made? Verify your answer.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// Create object and key pairs to populate dictionary

NSMutableString *key1 = [NSMutableString stringWithFormat: @"First key"];
NSMutableString *key2 = [NSMutableString stringWithFormat: @"Second key"];
NSMutableString *key3 = [NSMutableString stringWithFormat: @"Third key"];
NSMutableString *obj1 = [NSMutableString stringWithFormat: @"First object"];
NSMutableString *obj2 = [NSMutableString stringWithFormat: @"Second object"];
NSMutableString *obj3 = [NSMutableString stringWithFormat: @"Third object"];

// Create dictionary and copies

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: obj1, key1, obj2, key2, obj3, key3, nil];
NSMutableDictionary *mutableDict;
NSDictionary *immutableDict;

mutableDict = [dict mutableCopy];
immutableDict = [dict copy];

// Change the first object and print dictionaries to see results

[obj1 setString: @"This isn't the same as it was before"];

NSLog(@"Original dictionary %@\n\n", dict);
NSLog(@"Mutable dictionary %@\n\n", mutableDict);
NSLog(@"Immutable dictionary %@\n\n", immutableDict);

NSLog(@"The first object in the dictionaries change when the original is changed therefore they are shallow copies.");

[mutableDict release];
[immutableDict release];

[pool drain];
    return 0;
}

That's my code for the solution to the problem. As expected the copies are shallow.

One issue however, if I use the code

Code: (Objective-C)
[key1 setString: @"A different key"];

in my program right where I change the object it's associated with, the key in the dictionary objects does not change the way I'd expect it to.

Is there some reason that a key cannot be changed while the objects can?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: May 09, 2010, 08:17:17 PM

I don't think I mentioned in the text that the keys are copied while the objects are retained (or was that the point of the exercise)?

Cheers,

Steve
Logged
MarkReid
Full Member
***
Posts: 173






Reply #2 on: May 10, 2010, 03:01:25 AM

Thanks Steve, it didn't occur to me that would be possible so thought it was a problem with the code but it makes sense to me now.
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.