Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code is not telling the full story. I have replicated your code in a new cocoa application. There are a few minor differences, however the logic remains unchanged.</p> <p>My <code>AppDelegate.h</code> looks like this:</p> <pre><code>#import &lt;Cocoa/Cocoa.h&gt; @interface AppDelegate : NSObject &lt;NSApplicationDelegate&gt; @property (nonatomic) double time; @property (assign) IBOutlet NSWindow *window; @end </code></pre> <p><code>AppDelegate.m</code> looks like this:</p> <pre><code>#import "AppDelegate.h" @implementation AppDelegate @synthesize window = _window; @synthesize time; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSDate * startTime = [NSDate date]; time = [[NSDate date] timeIntervalSinceDate: startTime]; NSLog(@"Return time %f",[self returnTime]); NSLog(@"Log time %f", [self logTime]); } - (double) returnTime { return time; } - (double) logTime { return time; } @end </code></pre> <p>Copy this code into a new cocoa project and run the app and you will see this runs correctly. On my machine, the console output looks like this:</p> <pre><code>2012-06-21 20:02:17.832 test[298:403] Return time 0.000003 2012-06-21 20:02:17.833 test[298:403] Log time 0.000003 </code></pre> <p>There are a number of things that could be wrong with your code but most of these are just speculation without seeing any further code. First, You could have multiple instances of <code>mainViewController</code> as mention by <em>Phillip Mills</em> (you could have an instance in a nib file and also be passing in an instances the nibs file owner for example) which means you are calling methods which are passing back different time variables.</p> <p>You also say your application is continually updating the time variable. First of all, what triggers this update? Secondly, if the variable is being continuously updated it is unlikely to return the same value as the variable could have been updated between the two method calls.</p> <p>Note: there was also a small error in your code. This line:</p> <pre><code>NSLog ("for some reason, this one is working and returns a value %f", time); </code></pre> <p>should be (notice the @ sign):</p> <pre><code>NSLog (@"for some reason, this one is working and returns a value %f", time); </code></pre> <p>I went ahead and edited your post to correct this as I don't think this is the problem as the code won't even compile on my machine if the @ symbol is ommited.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload