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="JoshMcg" data-source="post: 773304" data-attributes="member: 57811"><p>I think I may have tried that last night, but when I do the program locks up on the second button click. Here is the code:</p><p></p><p>[code]</p><p></p><p>#import <Cocoa/Cocoa.h></p><p></p><p></p><p>@interface control : NSObject {</p><p> IBOutlet id textBox;</p><p> int a;</p><p> int b;</p><p> int c; </p><p> int press;</p><p> int firstPress;</p><p> NSMutableString *list;</p><p>}</p><p></p><p>- (IBAction)generate:(id)sender;</p><p>- (int)nextNumber;</p><p></p><p>@end</p><p></p><p></p><p>#import "control.h"</p><p></p><p></p><p>@implementation control</p><p>- (IBAction)generate:(id)sender </p><p>{</p><p> if (!firstPress) // print first number in sequence without comma</p><p> {</p><p> firstPress = 1;</p><p> list = [NSMutableString stringWithFormat:@"%d", [self nextNumber]];</p><p> [textBox setString:list];</p><p> }</p><p> else // print next number in fibonacci sequence with comma before each number</p><p> {</p><p> [list appendFormat:@", %d",[self nextNumber]];</p><p> [textBox setString:list];</p><p> }</p><p> </p><p>}</p><p></p><p>- (int)nextNumber</p><p>{</p><p> if (!press) // for the first press return 0 because it is the first number in the sequence</p><p> {</p><p> press = 1;</p><p> return 0;</p><p> }</p><p> else if (press == 1) // for the second press return 1 because that is the second number in the sequence</p><p> {</p><p> press = 2;</p><p> return 1;</p><p> }</p><p> else if (press == 2) // for the third press set a and b equal to the first and second number in the sequence</p><p> {</p><p> press = 3;</p><p> a = 0;</p><p> b = 1;</p><p> }</p><p> else // set a and b as the most recently calculated numbers in the sequence</p><p> {</p><p> a = b;</p><p> b = c;</p><p> }</p><p> c = a + b; // determine next value in the sequence by adding the most recent two values</p><p> return c; </p><p>}</p><p></p><p>@end</p><p></p><p>[/code]</p><p></p><p>The debugger loads all this into the window on the second click. I have never seen it before.</p><p></p><p>[code]</p><p></p><p>[Session started at 2008-12-18 11:24:27 -0500.]</p><p></p><p>[Session started at 2008-12-18 11:24:34 -0500.]</p><p>Loading program into debugger…</p><p>GNU gdb 6.3.50-20050815 (Apple version gdb-768) (Tue Oct 2 04:07:49 UTC 2007)</p><p>Copyright 2004 Free Software Foundation, Inc.</p><p>GDB is free software, covered by the GNU General Public License, and you are</p><p>welcome to change it and/or distribute copies of it under certain conditions.</p><p>Type "show copying" to see the conditions.</p><p>There is absolutely no warranty for GDB. Type "show warranty" for details.</p><p>This GDB was configured as "i386-apple-darwin".Program loaded.</p><p>sharedlibrary apply-load-rules all</p><p>Attaching to program: `/Users/Josh/My Documents/Apps/Fibonacci/build/Debug/Fibonacci.app/Contents/MacOS/Fibonacci', process 1343.</p><p>(gdb) </p><p>[/code]</p></blockquote><p></p>
[QUOTE="JoshMcg, post: 773304, member: 57811"] I think I may have tried that last night, but when I do the program locks up on the second button click. Here is the code: [code] #import <Cocoa/Cocoa.h> @interface control : NSObject { IBOutlet id textBox; int a; int b; int c; int press; int firstPress; NSMutableString *list; } - (IBAction)generate:(id)sender; - (int)nextNumber; @end #import "control.h" @implementation control - (IBAction)generate:(id)sender { if (!firstPress) // print first number in sequence without comma { firstPress = 1; list = [NSMutableString stringWithFormat:@"%d", [self nextNumber]]; [textBox setString:list]; } else // print next number in fibonacci sequence with comma before each number { [list appendFormat:@", %d",[self nextNumber]]; [textBox setString:list]; } } - (int)nextNumber { if (!press) // for the first press return 0 because it is the first number in the sequence { press = 1; return 0; } else if (press == 1) // for the second press return 1 because that is the second number in the sequence { press = 2; return 1; } else if (press == 2) // for the third press set a and b equal to the first and second number in the sequence { press = 3; a = 0; b = 1; } else // set a and b as the most recently calculated numbers in the sequence { a = b; b = c; } c = a + b; // determine next value in the sequence by adding the most recent two values return c; } @end [/code] The debugger loads all this into the window on the second click. I have never seen it before. [code] [Session started at 2008-12-18 11:24:27 -0500.] [Session started at 2008-12-18 11:24:34 -0500.] Loading program into debugger… GNU gdb 6.3.50-20050815 (Apple version gdb-768) (Tue Oct 2 04:07:49 UTC 2007) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-apple-darwin".Program loaded. sharedlibrary apply-load-rules all Attaching to program: `/Users/Josh/My Documents/Apps/Fibonacci/build/Debug/Fibonacci.app/Contents/MacOS/Fibonacci', process 1343. (gdb) [/code] [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Objective - c program help (source included)
Top