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: 772408" data-attributes="member: 57811"><p>I got it working using setters and getters, but the compiler still gives me a bunch of warnings. I know this isn't the best code, but am I at least on the right track? The textbox outlet is there because I would like to eventually have it display the results in the app window rather than the console but for now I am just trying one thing at a time. Here is the working code, sorry for the sloppiness. Originally I setup a new class to handle the setting and getting of the variables, but for some reason it didn't work. Is that the correct way to do it?</p><p></p><p>[CODE]</p><p>#import <Cocoa/Cocoa.h></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 buttonPress;</p><p>}</p><p></p><p>- (IBAction)generate:(id)sender;</p><p>- (void) seta: (int) newNumber;</p><p>- (void) setb: (int) newNumber;</p><p>- (int) a;</p><p>- (int) b;</p><p></p><p>@end</p><p></p><p>#import "control.h"</p><p></p><p>@implementation control</p><p>- (IBAction)generate:(id)sender </p><p>{</p><p> </p><p> if (buttonPress != 1)</p><p> {</p><p> buttonPress = 1;</p><p> int a = 0;</p><p> int b = 1;</p><p> int c;</p><p></p><p> NSLog(@"%d", a); //print first </p><p> NSLog(@"%d", b); //print second</p><p> c = a + b;</p><p> NSLog(@"%d", c); //print third</p><p> </p><p> int i;</p><p> for (i=0; i<=9; i++) //print #4-#13 of the sequence</p><p> {</p><p> a = b;</p><p> b = c;</p><p> c = a + b;</p><p> NSLog(@"%d", c);</p><p></p><p> }</p><p> [self seta: a];</p><p> [self setb: b];</p><p> [self setc: c];</p><p> }</p><p> </p><p> else // print one additional number in the sequence per button press</p><p> {</p><p> a = [self a];</p><p> b = [self b];</p><p> c = [self c];</p><p> a = b;</p><p> b = c;</p><p> c = a + b;</p><p> NSLog(@"%d", c);</p><p> }</p><p> </p><p>}</p><p></p><p>- (int) a</p><p>{</p><p> return a;</p><p>}</p><p></p><p>- (int) b</p><p>{</p><p> return b;</p><p>}</p><p></p><p>- (int) c</p><p>{</p><p> return c;</p><p>}</p><p></p><p>- (void) setc: (int) newNumber</p><p>{</p><p> c = newNumber;</p><p>}</p><p></p><p></p><p>- (void) seta: (int) newNumber</p><p>{</p><p> a = newNumber;</p><p>}</p><p></p><p>- (void) setb: (int) newNumber</p><p>{</p><p> b = newNumber;</p><p>}</p><p></p><p>@end</p><p></p><p>[/CODE]</p></blockquote><p></p>
[QUOTE="JoshMcg, post: 772408, member: 57811"] I got it working using setters and getters, but the compiler still gives me a bunch of warnings. I know this isn't the best code, but am I at least on the right track? The textbox outlet is there because I would like to eventually have it display the results in the app window rather than the console but for now I am just trying one thing at a time. Here is the working code, sorry for the sloppiness. Originally I setup a new class to handle the setting and getting of the variables, but for some reason it didn't work. Is that the correct way to do it? [CODE] #import <Cocoa/Cocoa.h> @interface control : NSObject { IBOutlet id textBox; int a; int b; int c; int buttonPress; } - (IBAction)generate:(id)sender; - (void) seta: (int) newNumber; - (void) setb: (int) newNumber; - (int) a; - (int) b; @end #import "control.h" @implementation control - (IBAction)generate:(id)sender { if (buttonPress != 1) { buttonPress = 1; int a = 0; int b = 1; int c; NSLog(@"%d", a); //print first NSLog(@"%d", b); //print second c = a + b; NSLog(@"%d", c); //print third int i; for (i=0; i<=9; i++) //print #4-#13 of the sequence { a = b; b = c; c = a + b; NSLog(@"%d", c); } [self seta: a]; [self setb: b]; [self setc: c]; } else // print one additional number in the sequence per button press { a = [self a]; b = [self b]; c = [self c]; a = b; b = c; c = a + b; NSLog(@"%d", c); } } - (int) a { return a; } - (int) b { return b; } - (int) c { return c; } - (void) setc: (int) newNumber { c = newNumber; } - (void) seta: (int) newNumber { a = newNumber; } - (void) setb: (int) newNumber { b = newNumber; } @end [/CODE] [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Objective - c program help (source included)
Top