Difference between self.ivar and ivar?

后端 未结 4 1116
遇见更好的自我
遇见更好的自我 2020-11-22 12:11
aclass.h

@interface aClass : NSObject {
    NSString *name;
}

@property (nonatomic, retain) IBOutlet NSString *name;

@end

aclass.m         


        
4条回答
  •  孤城傲影
    2020-11-22 13:05

    self.name uses the accessor and/or mutator defined by you (this is nonatomic and retain in your case). So when you call self.name = foo, it will call the setName:(NSString *)str mutator generated by the compiler, which will first release the current string, then retains the new string and finally sets name to the retained string.

    Just calling name = foo does nothing more than assigning name to foo.

    This also means that you can only call self.xxx when you have defined a property for the ivar, otherwise the compiler will tell you that it doesn't know about it(iVar).

提交回复
热议问题