前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >探寻iOS之协议(protocol)

探寻iOS之协议(protocol)

作者头像
sweet说好的幸福
发布2020-12-23 09:54:52
1.6K0
发布2020-12-23 09:54:52
举报
文章被收录于专栏:sweet_iOS

在iOS开发中,Protocol是一种经常用到的设计模式,苹果的系统框架中也普遍用到了这种方式,比如UITableView中的<UITableViewDelegate>,以及<NSCopying>、<NSObject>这样的协议。我想大家也都自定义过协议,一般都用于回调,或者数据传递。

Protocol是什么?

代码语言:javascript
复制
A protocol declares a programmatic interface that any class may choose to implement. Protocols make it possible for two classes distantly related by inheritance to communicate with each other to accomplish a certain goal. They thus offer an alternative to subclassing. Any class that can provide behavior useful to other classes may declare a programmatic interface for vending that behavior anonymously. Any other class may choose to adopt the protocol and implement one or more of its methods, thereby making use of the behavior. The class that declares a protocol is expected to call the methods in the protocol if they are implemented by the protocol adopter.

协议是任何类都能够选择实现的程序接口。协议能够使两个没有继承关系的类相互交流并完成特定的目的,因此它提供了除继承外的另一种选择。任何能够为其他类提供有用行为的类都能够声明接口来匿名的传达这个行为。任何其他类都能够选择遵守这个协议并实现其中的一个或多个方法,从而利用这个行为。如果协议遵守者实现了协议中的方法,那么声明协议的类就能够通过遵守者调用协议中的方法。

总结:

  • 协议能够声明方法,协议遵守者实现协议中的方法,声明协议的类通过遵守者调用协议中的方法;
  • protocol不能定义成员变量,但是能够声明属性,因为属性=成员变量+setting方法+getting方法;

问题: 在定义protocol的时候后面会有<NSObject>,为什么? e.g.

代码语言:javascript
复制
@protocol MGSwipeTableCellDelegate <NSObject>
@optional

首先要注意,NSObject是所有object-C的根类,<NSObject>是NSObject遵循的协议,协议也能继承,既可以继承自自定义的协议,也可以继承自系统的协议。那自定义protocol的时候直接让protocol继承<NSObject>这个协议呢?因为这个协议中定义了一些基本的方法,由于我们使用的所有类都继承NSObject这个基类,而这个基类遵守了<NSObject>这个协议,那么也就实现了其中的那些方法,这些方法当然可以由NSObject及其子类对象调用,但是在不知道遵守者类型的时候需要用到id <协议名>这样的指针,这个指针在编译期并不知道自己指向哪个对象,唯一能调用的便是协议中的方法,然而有时候又需要用一些基本的方法,比如要辨别id <协议名>这个指针所指的对象属于哪个类,就要用到-isMemberOf:这个方法,而这个方法是<NSObject>这个协议中的方法之一,所以,我们自定义的协议都需要继承<NSObject>。本段一开始便说道:<NSObject>中的方法在NSObject基类中实现了,那么无需再关心实现了,直接调用<NSObject>中的方法吧。

protocol 的分类:“正式协议” 和 “非正式协议”(类别)

对于protocol的分类,一般我们讨论protocol就是谈论正式协议。 对于protocol,iOS 文档是这样定义的:

代码语言:javascript
复制
There are two varieties of protocol, formal and informal:

A formal protocol declares a list of methods that client classes are expected to implement. Formal protocols have their own declaration, adoption, and type-checking syntax. You can designate methods whose implementation is required or optional with the @required and @optional keywords. Subclasses inherit formal protocols adopted by their ancestors. A formal protocol can also adopt other protocols. Formal protocols are an extension to the Objective-C language.

An informal protocol is a category on NSObject, which implicitly makes almost all objects adopters of the protocol. (A category is a language feature that enables you to add methods to a class without subclassing it.) Implementation of the methods in an informal protocol is optional. Before invoking a method, the calling object checks to see whether the target object implements it. Until optional protocol methods were introduced in Objective-C 2.0, informal protocols were essential to the way Foundation and AppKit classes implemented delegation.

非正式协议

非正式协议简单理解为类别,凡是NSObject或其子类的类别,都是非正式协议。 e.g.

代码语言:javascript
复制
@interface NSString (CamelCase)  //类别  
-(NSString*) camelCaseString;    
@end    

上面就定义了一个NSString的类别。

重要协议

下面介绍几个重要的系统定义的协议NSObject协议、NSCopying协议、NSMutableCopying协议。

<NSObject>

代码语言:javascript
复制
@protocol NSObject

- (BOOL)isEqual:(id)object;
@property (readonly) NSUInteger hash;

@property (readonly) Class superclass;
- (Class)class OBJC_SWIFT_UNAVAILABLE("use 'anObject.dynamicType' instead");
- (instancetype)self;

- (id)performSelector:(SEL)aSelector;
- (id)performSelector:(SEL)aSelector withObject:(id)object;
- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

- (BOOL)isProxy;

- (BOOL)isKindOfClass:(Class)aClass;
- (BOOL)isMemberOfClass:(Class)aClass;
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;

- (BOOL)respondsToSelector:(SEL)aSelector;

- (instancetype)retain OBJC_ARC_UNAVAILABLE;
- (oneway void)release OBJC_ARC_UNAVAILABLE;
- (instancetype)autorelease OBJC_ARC_UNAVAILABLE;
- (NSUInteger)retainCount OBJC_ARC_UNAVAILABLE;

- (struct _NSZone *)zone OBJC_ARC_UNAVAILABLE;

@property (readonly, copy) NSString *description;
@optional
@property (readonly, copy) NSString *debugDescription;

@end

注意到没,NSObject定义了两个readonly关键字的属性hash和superclass。因为上文提到过,protocol不能定义成员变量,但是能够声明属性,因为属性=成员变量+setting方法+getting方法

<NSCopying>

代码语言:javascript
复制
@protocol NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; // NS_DESIGNATED_INITIALIZER

@end

NSCopying是一个与对象拷贝有关的协议。如果想让一个类的对象支持拷贝,就需要让该类实现NSCopying协议。NSCopying协议中的声明的方法只有一个- (id)copyWithZone:(NSZone *)zone。当我们的类实现了NSCopying协议,通过类的对象调用copy方法时,copy方法就会去调用我们实现的- (id)copyWithZone:(NSZone *)zone方法,实现拷贝功能。实现代码如下所示:

代码语言:javascript
复制
- (id)copyWithZone:(NSZone *)zone{    
        PersonModel *model = [[[self class] allocWithZone:zone] init];
        model.firstName = self.firstName;
        model.lastName  = self.lastName;
        //未公开的成员
     model->_nickName = _nickName;
     return model;
}

tips:

  • 在- (id)copyWithZone:(NSZone *)zone方法中,一定要通过[self class]方法返回的对象调用allocWithZone:方法。因为指针可能实际指向的是PersonModel的子类。这种情况下,通过调用[self class],就可以返回正确的类的类型对象。

<NSMutableCopying>

代码语言:javascript
复制
@protocol NSMutableCopying

- (id)mutableCopyWithZone:(nullable NSZone *)zone;

@end

NSCopying协议与NSMutableCopying的区别主要是在于,返回的对象是否是可变类型的。 NSCopying协议与NSMutableCopying的区别主要是在于,返回的对象是否是可变类型的。以Foundation框架的NSArray为例

代码语言:javascript
复制
NSArray *nameArray = @[@"Jim", @"Tom", @"David"];
NSArray *copyArray = [nameArray copy];
NSMutableArray *mutableCopyArray = [nameArray mutableCopy];
[mutableCopyArray addObject:@"Sam"];

NSArray对象调用copy方法时,copy方法会调用- (id)copyWithZone:(NSZone *)zone,得到对象的一份拷贝,但得到的对象还是不可变的对象。而NSArray对象调用mutableCopy方法时,mutableCopy方法会调用- (id)mutableCopyWithZone:(NSZone *)zone,得到可变的对象。

所以,如果自定义类具有可变和不可变的区别,想让它支持拷贝时,就需要同时实现NSCopying和NSMutableCopying,在- (id)copyWithZone:(NSZone *)zone返回的是不可变对象,在- (id)mutableCopyWithZone:(NSZone *)zone返回的是可变对象。例如NSArray,在定义的时候需要申明支持NSCopying和NSMutableCopying协议,在NSArray中实现- (id)copyWithZone:(NSZone *)zone,在NSMutableArray中实现- (id)mutableCopyWithZone:(NSZone *)zone。

NSSecureCoding

代码语言:javascript
复制
// Objects which are safe to be encoded and decoded across privilege boundaries should adopt NSSecureCoding instead of NSCoding. Secure coders (those that respond YES to requiresSecureCoding) will only encode objects that adopt the NSSecureCoding protocol.
// NOTE: NSSecureCoding guarantees only that an archive contains the classes it claims. It makes no guarantees about the suitability for consumption by the receiver of the decoded content of the archive. Archived objects which  may trigger code evaluation should be validated independently by the consumer of the objects to verify that no malicious code is executed (i.e. by checking key paths, selectors etc. specified in the archive).

@protocol NSSecureCoding <NSCoding>
@required
// This property must return YES on all classes that allow secure coding. Subclasses of classes that adopt NSSecureCoding and override initWithCoder: must also override this method and return YES.
// The Secure Coding Guide should be consulted when writing methods that decode data.
#if FOUNDATION_SWIFT_SDK_EPOCH_AT_LEAST(8)
@property (class, readonly) BOOL supportsSecureCoding;
#else
+ (BOOL)supportsSecureCoding;
#endif
@end
  • 苹果在iOS6引入的基于NSCoding的一个新的协议,该协议能够保证有关归档代码的安全性。
  • 大部分支持NSCoding的系统对象都已经升级到支持NSSecureCoding;
  • 常用于对象编解码;

致谢

感谢雨雪传奇作品 感谢黄龙辉作品

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Protocol是什么?
  • protocol 的分类:“正式协议” 和 “非正式协议”(类别)
    • 非正式协议
    • 重要协议
      • <NSObject>
        • <NSCopying>
          • <NSMutableCopying>
            • NSSecureCoding
            • 致谢
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档