March 25, 2006
Help me learn Cocoa/Obj-C, vol. 1
So I did the Challenge problem in Chapter 4 of Cocoa Programming for Mac OS X, Second Edition. I've come up with two different "solutions". Solution 1: "Screw retain counts"- (IBAction) reportCharacterCount: (id)sender
{
NSString *inputString = [inputField stringValue];
[outputField setStringValue:[NSString stringWithFormat:@"%@ has %d letters.",
inputString,[inputString length]]];
}
Solution 2: "I'm a good boy"
- (IBAction) reportCharacterCount: (id)sender
{
NSString *inputString = [[NSString alloc] initWithString:[inputField stringValue]];
[outputField setStringValue:[NSString stringWithFormat:@"%@ has %d letters.",
inputString,[inputString length]]];
[inputString release];
}
For pedagogical reasons, could someone tell me what the difference is between the two? And if possible, which one is better?
(As you can probably guess, my very first solution consisted of version 1 with a release, which brought me to the debugger in a hurry.)
UPDATE: The magically Delicious Wil Shipley answered my question. Thanks Wil! (And I promise those blank lines aren't there in my actual source code file.)
UPDATE 2: The ever-helpful Jussi Hagman linked me to this article.Posted by Jeffrey at March 25, 2006 11:35 AM
What is a TrackBack? Learn more here. TrackBack URL for this entry:
http://www.geekable.com/cgi-bin/mt-tb.cgi/1096
Listed below are links to the 0 weblogs that reference 'Help me learn Cocoa/Obj-C, vol. 1' from Geekable.com.
http://www.geekable.com/cgi-bin/mt-tb.cgi/1096
Listed below are links to the 0 weblogs that reference 'Help me learn Cocoa/Obj-C, vol. 1' from Geekable.com.

