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: 773001" data-attributes="member: 57811"><p>Thanks a lot for your help. This is what I came up with. I changed it a little so that it generates one number in the sequence per button press rather than generating the first 13 with one press.</p><p></p><p>[CODE]</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>}</p><p></p><p>- (IBAction)generate:(id)sender;</p><p>- (int)nextNumber;</p><p></p><p>@end</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> NSLog(@"%d",[self nextNumber]); // print next number in fibonacci sequence</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>I do have a few more questions. </p><p></p><p>Is it okay to cut the nextNumber method short by returning values in the if statements? </p><p></p><p>If I was going to create a seperate fibonacci class would I have to initialize and alloc an instance of that class before I use it? Then all the work would be done by a single instance of the class. Is that right?</p><p></p><p>Rather than using NSLog to display the numbers I would like to output them using the textBox outlet. It is linked to a large text area in my GUI. I know that if I just use setStringValue it sets it to the new value and deletes the old value. Would I use a mutable string to add the new number values to the end of the string as they are created and output that?</p><p></p><p>Sorry about all the questions. I just finished coding and havn't done any research yet. I will edit my post if I find the answers I am looking for. </p><p></p><p>I am going to take your advice about doing tutorials for now to get a better understanding rather then trying my own projects. </p><p></p><p>Thanks again,</p><p></p><p>Josh</p></blockquote><p></p>
[QUOTE="JoshMcg, post: 773001, member: 57811"] Thanks a lot for your help. This is what I came up with. I changed it a little so that it generates one number in the sequence per button press rather than generating the first 13 with one press. [CODE] #import <Cocoa/Cocoa.h> @interface control : NSObject { IBOutlet id textBox; int a; int b; int c; int press; } - (IBAction)generate:(id)sender; - (int)nextNumber; @end #import "control.h" @implementation control - (IBAction)generate:(id)sender { NSLog(@"%d",[self nextNumber]); // print next number in fibonacci sequence } - (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] I do have a few more questions. Is it okay to cut the nextNumber method short by returning values in the if statements? If I was going to create a seperate fibonacci class would I have to initialize and alloc an instance of that class before I use it? Then all the work would be done by a single instance of the class. Is that right? Rather than using NSLog to display the numbers I would like to output them using the textBox outlet. It is linked to a large text area in my GUI. I know that if I just use setStringValue it sets it to the new value and deletes the old value. Would I use a mutable string to add the new number values to the end of the string as they are created and output that? Sorry about all the questions. I just finished coding and havn't done any research yet. I will edit my post if I find the answers I am looking for. I am going to take your advice about doing tutorials for now to get a better understanding rather then trying my own projects. Thanks again, Josh [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Objective - c program help (source included)
Top