views:

40

answers:

4

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.

+2  A: 

Send the variable a retain message.

tob
+1  A: 

Do [myObject retain] or @property(nonatomic, retain) MyClass *myObject;

Toastor
+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
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
You can retain them wherever you first initialize, assign or use them. Before then, your instance objects are all nil pointers.
hotpaw2
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