iOS 理解@property和set&get方法

xiaoxiao2021-02-27  334

例如:

        @property (nonatomic, assign) int height; 

        Xcode自动生成 

        对应的set方法:- (void)setHeight:(int)height; 

        get方法:- (int)height;

注释: 

        其中p.height = 100; //实质是调用set方法 

         NSLog(@"height is", p.height); //实质是调用get方法

#import "ViewController.h" @interface ViewController () { NSInteger aAge; } @property (nonatomic,assign) NSInteger age; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.age = 18; NSInteger myAge = self.age; } -(void)setAge:(NSInteger)age{ NSLog(@"++++++++++++++"); aAge = age; } -(NSInteger)age{ NSLog(@"---------------"); return aAge; }

转载请注明原文地址: https://www.6miu.com/read-5336.html

最新回复(0)