What are Setters and Getters?

前端 未结 2 345
既然无缘
既然无缘 2021-01-14 03:20

I don\'t really have a solid understanding of Setters and Getters for objective-c. Can someone provide a good guide for beginners? I noticed that this comes into play when t

相关标签:
2条回答
  • 2021-01-14 03:37

    The particular section of interest to you is Declared Properties.

    b's interface should look like:

    @interface b : NSObject {
        NSString *value;
    }
    
    @property (nonatomic, retain) NSString *value;
    
    - (id) initWithValue:(NSString *)newValue;
    
    @end
    

    Your implementation of b should look something like:

    @implementation b
    
    @synthesize value;
    
    - (id) initWithValue:(NSString *)newValue {
        if (self != [super init])
            return nil;
    
        self.value = newValue;
    
        return self;
    }
    
    @end
    

    Which you could then call like:

    b *test = [[b alloc] initWithValue:@"Test!"];
    NSLog(@"%@", test.value);
    
    0 讨论(0)
  • 2021-01-14 03:50

    The Getting Started with iOS guide in the iOS Reference Library outlines the reading material you should go through to nail down basics like this. Apple's guides are clearly written and thorough, and you'll be doing yourself a huge favor by hunkering down and just reading them.

    0 讨论(0)
提交回复
热议问题