Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 17, 2017, 10:44:59 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Exercise 3  (Read 2083 times)
Brabbeldas
Newbie
*
Posts: 10


« on: January 26, 2012, 07:35:28 AM »

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        
        // Create initial dictionary //
        NSMutableDictionary *states = [NSMutableDictionary dictionary];
        [states setObject:[NSMutableString stringWithString:@"CapitalA"] forKey:@"StateA"];
        [states setObject:[NSMutableString stringWithString:@"CapitalB"] forKey:@"StateB"];
        [states setObject:[NSMutableString stringWithString:@"CapitalC"] forKey:@"StateC"];
        
        // Create both immutable and mutable copy //
        NSDictionary *statesImCopy = [states copy];
        NSMutableDictionary *statesMCopy = [states mutableCopy];
        
        
        // Retrieve the first object from initial dictionary //  
        NSMutableString *object1 = [states objectForKey:@"StateA"];
        // Adjust this particular object //
        [object1 setString:@"CapitalAAA"];
        
        // Display initial dictionary //
        NSLog(@"Initial: %@", states);
        // Display both copies //
        NSLog(@"Immutable copy: %@", statesImCopy);
        NSLog(@"Mutable copy: %@", statesMCopy);
        
        
    }
    return 0;
}

The above shows that the first object in both copies are also changed, therefore: shallow copy
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.