iOS Protocol、Category中声明属性
之前一直有一个误区,认为协议和分类中不能用 @property 形式声明属性,现在做一下总结:
iOS中协议中和分类中是可以用
@property形式声明属性的,
只不过在协议、分类中声明的属性,只有对应的setter/getter方法,并没有生成对应的成员变量。
因为协议中只可以声明方法,分类中只能声明方法和对应的实现。
那么如何在协议中用@property声明属性,然后在实现协议的类中可以用成员变量直接访问属性的值?
Protocol:
@protocol MyProtocol <NSObject>
@property (nonatomic,strong) NSString *myImage;
@end
实现类:
@interface ViewController : UIViewController<MyProtocol>
@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<MyProtocol>
@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 <objc/runtime.h>
@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<MyProtocol>
@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 来合成 成员变量
在iOS的面试中经常会被问到如何为分类添加属性,但是鲜少听人提过协议中添加属性的情况。
我们知道协议是一组特定功能或者行为的规范,这个概念和C#或者Java中的接口是一样的,
但是C#或者Java中的接口类中都可以定义属性的,那么在OC的协议中定义属性可不可以呢
image.png
方式一:在.m实现类中添加@synthesize speed;
@protocol FlyDelegate <NSObject>
@required
@property(nonatomic, assign) NSUInteger speed;
@end
@interface TestProtocolProperty : NSObject<FlyDelegate>
@end
@implementation TestProtocolProperty
@synthesize speed;
- (instancetype)init {
if (self = [super init]) {
}
return self;
}
@end
方式二:在.m实现文件中添加合成speed属性的成员变量_speed和对应的getter和setter方法
@protocol FlyDelegate <NSObject>
@required
@property(nonatomic, assign) NSUInteger speed;
@end
@interface TestProtocolProperty : NSObject<FlyDelegate> {
NSUInteger _speed;
}
@end
@implementation TestProtocolProperty
- (instancetype)init {
if (self = [super init]) {
}
return self;
}
- (void)setSpeed:(NSUInteger)speed {
_speed = speed;
}
- (NSUInteger)speed {
return _speed;
}
@end
其实iOS的UIKit中就有协议中声明属性的例子:
打开UIKit中的UIApplicationDelegate协议声明就可以看到属性window;
- (void)applicationDidEnterBackground:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
- (void)applicationWillEnterForeground:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
- (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
- (void)applicationProtectedDataDidBecomeAvailable:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
@property (nullable, nonatomic, strong) UIWindow *window NS_AVAILABLE_IOS(5_0);
结论:OC语言的协议里面是支持声明属性的,而在协议中声明属性其实和在其中定义方法一样只是声明了getter和setter方法,并没有具体实现,所以当这个协议属性修饰符为@ required时,如果不实现编译器就会报出警告,最简单的方式就是加上属性同步语句@synthesize propertyName;
思考:属性和方法其实都是一个事物的特性,协议正是描述某类行为和特性的一种规范,基于这个事实,所以在协议中定义属性是很符合道理的。之所以在iOS开发中很少看到有人这么使用过是因为,iOS开发中协议通常是被用作代理模式而存在的,并且如果在协议中定义了是属性,就必须在实现类中添加对属性自动同步或者手动添加属性实现代码
image.png