开发者

confused about when to use self. in objective c

i have the following class

@interface DetailMessageViewController : UIViewController<UITextFieldDelegate,MFMessageComposeViewControllerDelegate,UIScrollViewDelegate> {
NSString *messageid;
NSString *messagesenttime;
NSString *sendermobilenumber;

NewMessageAnimation *animation;
NSString *addseconds;
IBOutlet UITextField *replyfield;
IBOutlet UIButton *replybutton;
NSString *tempstring;
UIAlertView *messagesent;
NSTimer *checkifanimationplayed;

}

@property(nonatomic,retain) NSString *messageid;
@property(nonatomic,retain) NSString *messagesenttime;

-(IBAction)开发者_高级运维replytomessage:(id)sender;
-(NSString *)decryptstring:(NSString *)encryptedstring;
+ (NSData *)decodeBase64WithString:(NSString *)strBase64;
-(void) animateTextField: (UITextField*) textField up: (BOOL) up;
-(IBAction)goawaykeyboard;
-(NSString *)encryptstring:(NSString *)clearstring;
+(NSString *)returndestroytime;
@end

i am very confused about when to use self.(instance variable) in my implementation. i have never used self.ivar in my implentation so far. i did not release any of my instance variables in the dealloc method and i have not set my ivars to nil in my viewdidunload method. my program still works fine. one thing i know is - i use the property when i am trying to access or assign the ivars from outside the class.

when i code what is do is--i do "build and analyze". it gives me few errors now and then. i make sure i release all allocated ivars wherever possbile. Finally i make sure i dont have any blue errors from my build and analyze. should that be fine or should i compulsorily release all my ivars in the dealloc method and set the ivars to nil in my viewdidunload method.

. can some one point me in the right direction for memory management.


Basically, its very simple: you use self. if you want to access the property and leave away the self if you want to access the member variable. That said, accessing the property works by accessing its getter/setter.

So, to access your messageid by property, you would call self.messageid, to access it by member variable, you simply call messageid.

Now.. say, if you have your own constructor that passes a messageid to your class, you would most likely do something like

-(void) initWithMessageId:(NSString*) aMessageId {
  if (super initWithNibName:.....) {
    self.messageid = aMessageId;
    ...
  }
  return self;
}

what would happen if you would only do a

if (super initWithNibName:.....) {
    messageid = aMessageId;
    ...

instead of the self? You would access the member, therefore you would have to retain yourself to accomodate the property ("messageid = [aMessageId retain];").


In short, using "build and analyze" as your only method of leak prevention will not work. Here is most basic summary:

Using the keyword 'self' will call an automatically generated getter and setter. Aside from helping with memory management, properties can be great for reducing code duplication (i.e. if you write a custom setter or getter that performs 'extra' work) and can make refactoring code much easier.

Regardless of if you decide to use properties, you must release all retained data in dealloc (otherwise you will be leaking). Properties help by making 'retained' memory easy to identify (hint: release and nullify all 'retain' properties).

For more information on memory management, see here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜