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: 772827" data-attributes="member: 28538"><p>Given how you have structured your code, you do need persistent instance values rather than local variables in order to keep around the current sequence data values for calculating subsequent values on subsequent button pushes. Since they are only used internally, you don't really need accessor and mutator methods. I'd define a, b, and c as instance variables (although I'd probably choose more descriptive names) and then use them directly in the Fibonacci calculations, with no automatic (local) variables at all. The value of buttonPress also needs to be persistent, either as an instance variable (as in your 2nd example) or as a static local variable in the generate method.</p><p></p><p>I would suggest restructuring things to at least move the Fibonacci calculations out of generate and into a separate method (perhaps named something like getNextFibonacciValue) that simply calculates the next value in the sequence. Your generate method would then call getNextFibonacciValue, in order to keep the Fibonacci calculation in exactly one unique place. Duplication of algorithmic code in multiple locations invites problems in any non-trivial program, so it's wise to get used to designing your code that way from the outset. In fact I'd go further and say that the code and persistent values for the Fibonacci algorithm belong in a separate class object that is composited into the controller class, which hides its internal data (a, b, and c) and just provides the next Fibonacci number as a return value from getNextFibonacciValue, since the generate method doesn't really need to know anything more than that.</p><p></p><p>EDIT: This is short enough that I'll whip up some code to make it clearer, so stop reading now if you want to try it yourself first. Notice that the restructured code is quite concise. I didn't create a separate Fibonacci class for this example, but it would be similar in most respects.</p><p></p><p>[CODE]</p><p>@interface appController : NSWindowController {</p><p> int prev1;</p><p> int prev2;</p><p> int current;</p><p> int seqVal; </p><p>}</p><p>- (IBAction)generate:(id)sender;</p><p>- (int)getNextFibonacciValue;</p><p>@end</p><p></p><p>@implementation appController</p><p>- (IBAction)generate:(id)sender</p><p>{</p><p> static int initialSeqShown = 0;</p><p> if (!initialSeqShown)</p><p> {</p><p> for (int i = 0; i < 13; ++i)</p><p> NSLog(@"%d", [self getNextFibonacciValue]);</p><p> initialSeqShown = 1;</p><p> }</p><p> else</p><p> NSLog(@"%d", [self getNextFibonacciValue]);</p><p>}</p><p></p><p>- (int)getNextFibonacciValue</p><p>{</p><p> if (seqVal <= 1)</p><p> current = seqVal++;</p><p> else</p><p> {</p><p> prev2 = prev1;</p><p> prev1 = current;</p><p> current = prev1 + prev2; </p><p> }</p><p> return current;</p><p>}</p><p>@end</p><p></p><p>[/CODE]</p></blockquote><p></p>
[QUOTE="mystic_fm, post: 772827, member: 28538"] Given how you have structured your code, you do need persistent instance values rather than local variables in order to keep around the current sequence data values for calculating subsequent values on subsequent button pushes. Since they are only used internally, you don't really need accessor and mutator methods. I'd define a, b, and c as instance variables (although I'd probably choose more descriptive names) and then use them directly in the Fibonacci calculations, with no automatic (local) variables at all. The value of buttonPress also needs to be persistent, either as an instance variable (as in your 2nd example) or as a static local variable in the generate method. I would suggest restructuring things to at least move the Fibonacci calculations out of generate and into a separate method (perhaps named something like getNextFibonacciValue) that simply calculates the next value in the sequence. Your generate method would then call getNextFibonacciValue, in order to keep the Fibonacci calculation in exactly one unique place. Duplication of algorithmic code in multiple locations invites problems in any non-trivial program, so it's wise to get used to designing your code that way from the outset. In fact I'd go further and say that the code and persistent values for the Fibonacci algorithm belong in a separate class object that is composited into the controller class, which hides its internal data (a, b, and c) and just provides the next Fibonacci number as a return value from getNextFibonacciValue, since the generate method doesn't really need to know anything more than that. EDIT: This is short enough that I'll whip up some code to make it clearer, so stop reading now if you want to try it yourself first. Notice that the restructured code is quite concise. I didn't create a separate Fibonacci class for this example, but it would be similar in most respects. [CODE] @interface appController : NSWindowController { int prev1; int prev2; int current; int seqVal; } - (IBAction)generate:(id)sender; - (int)getNextFibonacciValue; @end @implementation appController - (IBAction)generate:(id)sender { static int initialSeqShown = 0; if (!initialSeqShown) { for (int i = 0; i < 13; ++i) NSLog(@"%d", [self getNextFibonacciValue]); initialSeqShown = 1; } else NSLog(@"%d", [self getNextFibonacciValue]); } - (int)getNextFibonacciValue { if (seqVal <= 1) current = seqVal++; else { prev2 = prev1; prev1 = current; current = prev1 + prev2; } return current; } @end [/CODE] [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Objective - c program help (source included)
Top