Chris
Chris

Reputation: 1363

How can I make sure nothing gets autoreleased?

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.

Upvotes: 0

Views: 76

Answers (4)

BoltClock
BoltClock

Reputation: 723428

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;

Upvotes: 0

Ben Gottlieb
Ben Gottlieb

Reputation: 85522

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.

Upvotes: 1

Toastor
Toastor

Reputation: 8990

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

Upvotes: 1

tobiasbayer
tobiasbayer

Reputation: 10369

Send the variable a retain message.

Upvotes: 2

Related Questions