I need a way to create variables that are accessible for the entire time my custom class is initiated to when I call the release function. I need to retain a NSDate and a NSString.
+1
A:
Do [myObject retain]
or @property(nonatomic, retain) MyClass *myObject;
Toastor
2010-08-27 12:45:07
+1
A:
Autorelease just fires a -release message at a later time. If you want your variables to stick around, -retain them when they're assigned. Even if they're autoreleased, your retain will increment the retainCount, so they will not be dealloced. Just be sure to -release them in YOUR dealloc.
Ben Gottlieb
2010-08-27 12:45:11
I have a string and a date, do I have to have an init method in my own class to send them a retain?
pki
2010-08-27 12:47:39
You can retain them wherever you first initialize, assign or use them. Before then, your instance objects are all nil pointers.
hotpaw2
2010-08-27 14:53:54
A:
Give your custom class retain properties for each variable in its header file:
@property (nonatomic, retain) NSDate *myDate;
@property (nonatomic, retain) NSString *myString;
Be sure to create proper setters and getters, or use @synthesize
, in its implementation file:
@synthesize myDate, myString;
BoltClock
2010-08-27 12:50:33