Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Objective - c program help (source included)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="mystic_fm" data-source="post: 773335" data-attributes="member: 28538"><p>First, I'd suggest replacing your declaration of textBox with this:</p><p></p><p>[CODE] IBOutlet NSTextField *textBox;[/CODE]</p><p></p><p>Also, you're not calling the correct selector on textBox; try setStringValue instead. (The NSTextField declaration above would have allowed the compiler to warn you about using the wrong selector, BTW.)</p><p></p><p>[CODE] [textBox setStringValue:list];[/CODE]</p><p></p><p>Third, I expect the error you encountered was because you never retained the NSMutableString object returned by stringWithFormat, as the latter returns an autoreleased object in the classic memory management mode (I'm assuming you haven't enabled the garbage collector on this test program). After your first iteration of "generate" completed and program control returned to the runtime loop, the autoreleased string object was automatically deleted. To fix this, you need to at least add the 2nd line below:</p><p></p><p>[CODE]</p><p>list = [NSMutableString stringWithFormat:@"%d", [self nextNumber]];</p><p></p><p>[list retain];[/CODE]</p><p></p><p>It would be safer to instantiate your NSMutableString in the init method (via alloc and init), rather than in one control path of your generate function. Even though that path ought to be the first one that's executed as the code is currently written, it's unwise to rely upon behavioral side effects like that. Using the init method would not only be safer, but would give you a retained string object that wouldn't give you the error you experienced.</p><p></p><p>Note that I'm not even touching upon the need to clean up this allocated string object when your program exits. You are getting into a number of areas here, such as Cocoa memory management, for which you need book(s) and a structured approach to learn your way around them; a few posts in a forum aren't going to suffice.</p></blockquote><p></p>
[QUOTE="mystic_fm, post: 773335, member: 28538"] First, I'd suggest replacing your declaration of textBox with this: [CODE] IBOutlet NSTextField *textBox;[/CODE] Also, you're not calling the correct selector on textBox; try setStringValue instead. (The NSTextField declaration above would have allowed the compiler to warn you about using the wrong selector, BTW.) [CODE] [textBox setStringValue:list];[/CODE] Third, I expect the error you encountered was because you never retained the NSMutableString object returned by stringWithFormat, as the latter returns an autoreleased object in the classic memory management mode (I'm assuming you haven't enabled the garbage collector on this test program). After your first iteration of "generate" completed and program control returned to the runtime loop, the autoreleased string object was automatically deleted. To fix this, you need to at least add the 2nd line below: [CODE] list = [NSMutableString stringWithFormat:@"%d", [self nextNumber]]; [list retain];[/CODE] It would be safer to instantiate your NSMutableString in the init method (via alloc and init), rather than in one control path of your generate function. Even though that path ought to be the first one that's executed as the code is currently written, it's unwise to rely upon behavioral side effects like that. Using the init method would not only be safer, but would give you a retained string object that wouldn't give you the error you experienced. Note that I'm not even touching upon the need to clean up this allocated string object when your program exits. You are getting into a number of areas here, such as Cocoa memory management, for which you need book(s) and a structured approach to learn your way around them; a few posts in a forum aren't going to suffice. [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Objective - c program help (source included)
Top