arrow operator in objective-c

前端 未结 3 1136
盖世英雄少女心
盖世英雄少女心 2020-12-11 01:09

I have a question, here\'s the code:

@interface MyFoo : NSObject {
    NSString *nameStr;
}
@end
@implementation MyFoo
- (id)init {
    self = [super init];
         


        
3条回答
  •  鱼传尺愫
    2020-12-11 01:47

    Using arrow notation on just the ivar name to access a property will not guarantee they will be retain, assign or etc ... Because you are directing accessing an ivar and not calling and setter ou getter method used in properties.

    Example:

    @interface MyFoo : NSObject {
    }
    @property(nonatomic,retain)  NSString *nameStr;
    @end
    @implementation MyFoo
    - (id)initWithString:(NSString *)name {
        self = [super init];
        if (self) {
            self->nameStr = name; // will not be retained
        }
        return self;
    }
    @end
    

    For ivar variables as already be answer there`s nothing wrong.

提交回复
热议问题