当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS Protocol、Category中声明属性方法详情

iOS Protocol、Category中声明属性方法详情

2018年03月07日  | 移动技术网移动技术  | 我要评论

之前一直有一个误区,认为协议和分类中不能用@property 形式声明属性,现在做一下总结:

iOS中协议中和分类中是可以用@property形式声明属性的,只不过在协议、分类中声明的属性,只有对应的setter/getter方法,并没有生成对应的成员变量。因为协议中只可以声明方法,分类中只能声明方法和对应的实现。
那么如何在协议中用@property声明属性,然后在实现协议的类中可以用成员变量直接访问属性的值?

Protocol:

@protocol MyProtocol 
@property (nonatomic,strong) NSString *myImage;
@end

实现类:

@interface ViewController : UIViewController
@end
@implementation ViewController
@synthesize myImage = _myImage;
- (void)viewDidLoad {
    [super viewDidLoad];
    self.myImage = @"my string";
    NSLog(@"%@,%@",_myImage,self.myImage);
@end

上面方法中主要用到了@synthesize 上面声明部分的 @synthesize myImage = _myImage; 意思是说,myImage 属性为 _myImage 成员变量合成访问器方法。 也就是说,myImage属性生成存取方法是setMyImage,这个setMyImage方法就是_myImage变量的存取方法,它操作的就是_myImage这个变量。通过这个看似是赋值的这样一个操作,我们可以在@synthesize 中定义与变量名不相同的getter和setter的命名,籍此来保护变量不会被不恰当的访问。比如完全可以写成:

@interface ViewController : UIViewController
@end
@implementation ViewController
@synthesize myImage = _helloWorld;  //set方法的值保存在_helloWorld中
- (void)viewDidLoad {
    [super viewDidLoad];
    self.myImage = @"my string";
    NSLog(@"%@,%@", _helloWorld,self.myImage);
    //self.myImage 取的就是成员变量_helloWorld的值;
@end

根据上面的结论那么Category中能否一样使用 @synthesize来保存成员变量的值呢?
只可惜Category中的implementation中不支持用@synthesize 来合成属性,正确的做法可以这样:
Category:

@interface NSObject (Extension)
@property (nonatomic,strong) NSString *myTitle;
@end

#import 
@implementation NSObject (Extension)
- (NSString *)myTitle {
    return objc_getAssociatedObject(self, @selector(myTitle));   
}
- (void)setMyTitle:(NSString *)myTitle {
    objc_setAssociatedObject(self, @selector(myTitle), myTitle,OBJC_ASSOCIATION_RETAIN);
}

这样在实现类中同样可以使用.xx获取存取的值:

@interface ViewController : UIViewController
@end
#import "NSObject+Extension.h"
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    NSObject *myObj = [[NSObject alloc] init];
    myObj.myTitle = @"my title";
    NSLog(@"%@",myObj.myTitle);
@end

补充:@dynamic 和 @synthesize的区别:
在@implementation 中通过@dynamic xxx 告诉编译器、xxx属性的setter、getter方法由开发者自己来生成
@ synthesize xxx = _xxx; 告诉编译器、xxx属性的setter、getter方法由编译器来生成、同时用_xxx 来合成 成员变量


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网