arrow operator in objective-c

前端 未结 3 1137
盖世英雄少女心
盖世英雄少女心 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:37

    Using the arrow notation isn't incorrect, and there is a difference between using the arrow and the dot notation.If you use the arrow operator you access to the instance variable, if you use the dot operator you access to the property.
    It doesn't work like C structs where you use the arrow notation to access to a member of a struct pointed, and dot notation to access the struct members. So I would make a significative example:

    @property (nonatomic, strong) NSString *text;
    

    In .m file:

    - (void)setText:(NSString *)string {
        NSLog(@"Accessing property");
        self->text = string; // let's suppose that text is not synthesized
    }
    

    If you use the dot notation , then you access to the property and it would print "Accessing property".But this hasn't to do with C structs syntax.

提交回复
热议问题