On 19.04.2012, at 19:43, The Rhythmic wrote:
> Am sorry to ask such a trivial question. Am a newbie to Objective-C, &
> simply cannot see how to get this working, after having tried several
> possible ways & google'd around for it. Please help!
> My question is simple. I have a class-level NSDate object, which is
> declared outside any method in the class as:
> 
> *    NSDate *fromDate;*

 There are no "class-level objects" in Objective C. There are only global 
variables (even if they are declared between @implementation and @end of a 
class), and instance variables (declared using @property and @synthesize, or 
declared between the curly brackets of the class. So if you google for "global 
variable", or "instance variable" you might find better information.

 But reading on, I think your main confusion is memory management. I presume 
you have not turned on ARC or the garbage collector, right?

> Now, within a method, am setting this value to the date from a DatePicker
> as:
> 
> *    fromDate = [datePicker date];*

 "date" does not contain "retain", "copy", "new" or "alloc", so it gives you an 
object you do not own. Usually an autoreleased object, which stays around until 
your method returns or thereabouts, but maybe it's just owned by this date 
picker. fromDate contains the address (think "building number" of one building 
in a street) of that particular date. Now when your date picker gets destroyed 
(e.g. you close its window) or when its value changes, the date picker releases 
the old date (and creates a new one in the case of the change).

 (Going with a house metaphor, this means it tears down the house whose house 
number you remembered, and forgets its house number, then goes to build another 
house containing the new date)

 Now, the problem is, your fromDate variable still contains the address of the 
old, destroyed date. So when you later use it in another method, you end up not 
at a date object, but in a random memory location (an empty building site, if 
you want).

> Soon after the above assignment, I print its value into the log & it works
> fine.
> 
> *    NSLog(@"From Date: %@", fromDate);*
> 
> Now, when I use NSDate's value in another/different method, the value's
> gone! Why is it not persisted across methods in the same class itself? What
> can I do for the value to be accessible across methods?

 Learn about memory management in Objective C, from this article:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.masters-of-the-void.com




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to