Amazon.com Widgets exercise 17-2
Welcome, Guest. Please login or register.
Did you miss your activation email?
June 18, 2013, 01:42:22 PM
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 17
| | | |-+  exercise 17-2
Pages: [1]   Go Down
Print
Author Topic: exercise 17-2  (Read 1187 times)
mdziedzic
Newbie
*
Posts: 41



WWW




« on: January 05, 2010, 07:57:18 PM »

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

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

NSMutableArray *treats = [NSMutableArray arrayWithCapacity: 3];
NSMutableString *str1 = [NSMutableString stringWithString: @"lemon curd tart"];
NSMutableString *str2 = [NSMutableString stringWithString: @"dark chocolate"];
NSMutableString *str3 = [NSMutableString stringWithString: @"peppermint ice cream"];
NSMutableString *str4 = [NSMutableString stringWithString: @"rhubarb pie"];

NSLog(@"str1 retain count = %x", [str1 retainCount]);
NSLog(@"str2 retain count = %x", [str2 retainCount]);
NSLog(@"str3 retain count = %x", [str3 retainCount]);
NSLog(@"str4 retain count = %x", [str4 retainCount]);

[treats addObject: str1];
[treats addObject: str2];
[treats addObject: str3];

NSLog(@"\n");
NSLog(@"after adding str1, str2, str3 to array treats:");
NSLog(@"str1 retain count = %x", [str1 retainCount]);
NSLog(@"str2 retain count = %x", [str2 retainCount]);
NSLog(@"str3 retain count = %x", [str3 retainCount]);
NSLog(@"str4 retain count = %x", [str4 retainCount]);

[treats replaceObjectAtIndex: 1 withObject: str4];

NSLog(@"\n");
NSLog(@"after replacing str2 with str4 in array treats:");
NSLog(@"str1 retain count = %x", [str1 retainCount]);
NSLog(@"str2 retain count = %x", [str2 retainCount]);
NSLog(@"str3 retain count = %x", [str3 retainCount]);
NSLog(@"str4 retain count = %x", [str4 retainCount]);

[pool drain];
    return 0;
}

str1 retain count = 1
str2 retain count = 1
str3 retain count = 1
str4 retain count = 1

after adding str1, str2, str3 to array treats:
str1 retain count = 2
str2 retain count = 2
str3 retain count = 2
str4 retain count = 1

after replacing str2 with str4 in array treats:
str1 retain count = 2
str2 retain count = 1
str3 retain count = 2
str4 retain count = 2

Logged
john67
Newbie
*
Posts: 32






« Reply #1 on: August 28, 2011, 11:39:11 AM »

Just want to point out to anyone copying this - there is a small error (which you most likely pick up by now if you've got this far in the book!!), doesn't stop the program from running but is the format specifier: %x should be %lx
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.