《编写高质量OC代码》已顺利完成一二三四五六七八篇!
附上连接:
iOS 编写高质量Objective-C代码(一)—— 简介
iOS 编写高质量Objective-C代码(二)—— 面向对象
iOS 编写高质量Objective-C代码(三)—— 接口和API设计
iOS 编写高质量Objective-C代码(四)—— 协议与分类
iOS 编写高质量Objective-C代码(五)—— 内存管理机制
iOS 编写高质量Objective-C代码(六)—— block专栏
iOS 编写高质量Objective-C代码(七)—— GCD专栏
iOS 编写高质量Objective-C代码(八)—— 系统框架html
这篇将从面向对象的角度分析如何提升OC的代码质量。git
属性(@property
)是OC的一项特性。 @property
:编译器会自动生成实例变量
和getter
和setter
方法。 For Example:swift
@property (nonatomic, strong) UIView *qiShareView;
复制代码
等价于:缓存
@synthesize qiShareView = _qiShareView;
- (UIView *)qiShareView;
- (void)setQiShareView:(UIView *)qiShareView;
复制代码
若是不但愿生成存取方法和实例变量,那就要使用@dynamic关键字安全
@dynamic qiShareView;
复制代码
属性特质有四类:bash
atomic
nonatomic
:非原子性,读写时不加同步锁atomic
:原子性,读写时加同步锁readwrite
readwrite
:读写,拥有getter
和setter
方法readonly
:只读,仅拥有getter
方法assign
:对“纯量类型”作简单赋值操做(NSInteger
、CGFloat
等)。strong
:强拥有关系,设置方法 保留新值,并释放旧值。weak
:弱拥有关系,设置方法 不保留新值,不释放旧值。当指针指向的对象销毁时,指针置nil
。copy
:拷贝拥有关系,设置方法不保留新值,将其拷贝。unsafe_unretained
:非拥有关系,目标对象被释放,指针不置nil
,这一点和assign
同样。区别于weak
getter=<name>
:指定get方法的方法名,经常使用setter=<name>
:指定set方法的方法名,不经常使用 例如:@property (nonatomic, getter=isOn) BOOL on;
复制代码
在iOS开发中,99.99..%的属性都会声明为nonatomic。 一是
atomic
会严重影响性能, 二是atomic
只能保证读/写操做的过程是可靠的,并不能保证线程安全。 关于第二点能够参考个人博客:iOS 为何属性声明为atomic依然不能保证线程安全?框架
init
和dealloc
方法中,老是应该经过访问实例变量读写数据getter
和setter
方法、也没有使用KVO
监听For Example:函数
- (instancetype)initWithDic:(NSDictionary *)dic {
self = [super init];
if (self) {
_qi = dic[@"qi"];
_share = dic[@"share"];
}
return self;
}
复制代码
getter/setter
方法(好比:懒加载)KVO
监听值的改变For Example:性能
- (UIView *)qiShareView {
if (!_qiShareView) {
_qiShareView = [UIView new];
}
return _qiShareView;
}
复制代码
思考下面输出什么?优化
NSString *aString = @"iPhone 8";
NSString *bString = [NSString stringWithFormat:@"iPhone %i", 8];
NSLog(@"%d", [aString isEqual:bString]);
NSLog(@"%d", [aString isEqualToString:bString]);
NSLog(@"%d", aString == bString);
复制代码
答案是110 ==操做符只是比较了两个指针所指对象的地址是否相同,而不是指针所指的对象的值 因此最后一个为0
为何下面这个例子的if永远为false?
id maybeAnArray = @[];
if ([maybeAnArray class] == [NSArray class]) {
//Code will never be executed
}
复制代码
由于[maybeAnArray class]
的返回永远不会是NSArray
,NSArray
是一个类族,返回的值一直都是NSArray的实体子类
。大部分collection类都是某个类族中的抽象基类
因此上面的if想要有机会执行的话要改为
id maybeAnArray = @[];
if ([maybeAnArray isKindOfClass [NSArray class]) {
// Code probably be executed
}
复制代码
这样判断的意思是,maybeAnArray
这个对象是不是NSArray
类族中的一员 使用类族的好处:能够把实现细节隐藏再一套简单的公共接口后面
先引入runtime类库
#import <objc/runtime.h>
复制代码
objc_AssociationPolicy(关联策略类型) | 等效的@property属性 |
---|---|
OBJC_ASSOCIATION_ASSIGN | 等效于 assign |
OBJC_ASSOCIATION_RETAIN_NONATOMIC | 等效于 nonatomic, retain |
OBJC_ASSOCIATION_COPY_NONATOMIC | 等效于 nonatomic, copy |
OBJC_ASSOCIATION_RETAIN | 等效于 retain |
OBJC_ASSOCIATION_COPY | 等效于 copy |
/**
* Sets an associated value for a given object using a given key and association policy.
*
* @param object The source object for the association.
* @param key The key for the association.
* @param value The value to associate with the key key for object. Pass nil to clear an existing association.
* @param policy The policy for the association. For possible values, see “Associative Object Behaviors.”
*/
OBJC_EXPORT void
objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
id _Nullable value, objc_AssociationPolicy policy)
复制代码
/**
* Returns the value associated with a given object for a given key.
*
* @param object The source object for the association.
* @param key The key for the association.
*
* @return The value associated with the key \e key for \e object.
*/
OBJC_EXPORT id _Nullable
objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
复制代码
/**
* Removes all associations for a given object.
*
* @param object An object that maintains associated objects.
*
* @note The main purpose of this function is to make it easy to return an object
* to a "pristine state”. You should not use this function for general removal of
* associations from objects, since it also removes associations that other clients
* may have added to the object. Typically you should use \c objc_setAssociatedObject
* with a nil value to clear an association.
*
*/
OBJC_EXPORT void
objc_removeAssociatedObjects(id _Nonnull object)
复制代码
小结:
首先咱们要区分两个基本概念: 1 .静态绑定(static binding):在
编译期
就能决定运行时所应调用的函数。~表明语言:C、C++等~ 2 .动态绑定 (dynamic binding):所要调用的函数直到运行期
才能肯定。~表明语言:OC、swift等~
OC是一门强大的动态语言,它的动态性体如今它强大的runtime机制
上。
解释:在OC中,若是向某对象传递消息,那就会使用动态绑定机制来决定须要调用的方法。在底层,全部方法都是普通的C语言函数,然而对象收到消息后,由运行期决定究竟调用哪一个方法,甚至能够在程序运行时改变,这些特性使得OC成为一门强大的动态语言
。
objc_msgSend
,定义以下:void objc_msgSend(id self, SEL cmd, ...)
复制代码
这是一个参数个数可变的函数,第一参数表明接受者,第二个参数表明选择子(OC函数名),以后的参数就是消息中传入的参数。
id return = [git commit:parameter];
复制代码
上面的方法会在运行时转换成以下的OC函数:
id return = objc_msgSend(git, @selector(commit), parameter);
复制代码
objc_msgSend
函数会在接收者所属的类中搜寻其方法列表,若是能找到这个跟选择子名称相同的方法,就跳转到其实现代码,往下执行。如果当前类没找到,那就沿着继承体系继续向上查找,等找到合适方法以后再跳转 ,若是最终仍是找不到,那就进入消息转发(下一条具体展开)的流程去进行处理了。
但是若是每次传递消息都要把类中的方法遍历一遍,这么多消息传递加起来确定会很耗性能。因此如下讲解OC消息传递的优化方法。
objc_msgSend
在搜索这块是有作缓存的,每一个OC的类都有一块这样的缓存,objc_msgSend
会将匹配结果缓存在快速映射表(fast map)
中,这样以来这个类一些频繁调用的方法会出如今fast map
中,不用再去一遍一遍的在方法列表中搜索了。首先区分两个基本概念: 1 .消息传递:对象正常解读消息,传递过去(见上一条)。 2 .消息转发:对象没法解读消息,以后进行消息转发。
流程解释:
resolveInstanceMethod
:征询接受者(所属的类)是否能够添加方法以处理未知的选择子?~(此过程称为动态方法解析)~如有,转发结束。若没有,走第二步。forwardingTargetForSelector
:询问接受者是否有其余对象能处理此消息。如有,转发结束,一切如常。若没有,走第三步。forwardInvocation
:运行期系统将消息封装到NSInvocation对象中,再给接受者一次机会。unrecognized selector sent to instance xxxx
方法调配(Method Swizzling):使用另外一种方法实现来替换原有的方法实现。~(实际应用中,经常使用此技术向原有实现中添加新的功能。)~
<objc/runtime.h>里的两个经常使用的方法:
/**
* Returns a specified instance method for a given class.
*
* @param cls The class you want to inspect.
* @param name The selector of the method you want to retrieve.
*
* @return The method that corresponds to the implementation of the selector specified by
* \e name for the class specified by \e cls, or \c NULL if the specified class or its
* superclasses do not contain an instance method with the specified selector.
*
* @note This function searches superclasses for implementations, whereas \c class_copyMethodList does not.
*/
OBJC_EXPORT Method _Nullable
class_getInstanceMethod(Class _Nullable cls, SEL _Nonnull name)
复制代码
/**
* Exchanges the implementations of two methods.
*
* @param m1 Method to exchange with second method.
* @param m2 Method to exchange with first method.
*
* @note This is an atomic version of the following:
* \code
* IMP imp1 = method_getImplementation(m1);
* IMP imp2 = method_getImplementation(m2);
* method_setImplementation(m1, imp2);
* method_setImplementation(m2, imp1);
* \endcode
*/
OBJC_EXPORT void
method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2)
复制代码
利用这两个方法就能够交换指定类中的指定方法。在实际应用中,咱们会经过这种方式为既有方法添加新功能。
For Example:交换method1与method2的方法实现
Method method1 = class_getInstanceMethod(self, @selector(method1:));
Method method2 = class_getInstanceMethod(self, @selector(method2:));
method_exchangeImplementations(method1, method2);
复制代码
Objective-C类是由Class类型来表示的,实质是一个指向objc_class结构体的指针。它的定义以下:
typedef struct objc_class *Class;
复制代码
在<objc/runtime.h>中能看到他的实现:
struct objc_class {
Class _Nonnull isa OBJC_ISA_AVAILABILITY; //!< 指向metaClass(元类)的指针
#if !__OBJC2__
Class _Nullable super_class OBJC2_UNAVAILABLE; //!< 父类
const char * _Nonnull name OBJC2_UNAVAILABLE; //!< 类名
long version OBJC2_UNAVAILABLE; //!< 类的版本信息,默认为0
long info OBJC2_UNAVAILABLE; //!< 类信息,供运行期使用的一些位标识
long instance_size OBJC2_UNAVAILABLE; //!< 该类的实例变量大小
struct objc_ivar_list * _Nullable ivars OBJC2_UNAVAILABLE; //!< 该类的成员变量链表
struct objc_method_list * _Nullable * _Nullable methodLists OBJC2_UNAVAILABLE; //!< 方法定义的链表
struct objc_cache * _Nonnull cache OBJC2_UNAVAILABLE; //!< 方法缓存表
struct objc_protocol_list * _Nullable protocols OBJC2_UNAVAILABLE; //!< 协议链表
#endif
} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */
复制代码
此结构体存放的是类的“元数据”(metadata),例如类的实例实现了几个方法,父类是谁,具有多少实例变量等信息。 这里的isa指针指向的是另一个类叫作元类(metaClass)。那什么是元类呢?元类是类对象的类。也能够换一种容易理解的说法:
runtime
处理时是在这个对象的类的方法列表中寻找runtime
处理时是在这个类的元类的方法列表中寻找咱们来看一个很经典的图来加深理解:
能够总结以下:
Class
都有一个isa指针
指向一个惟一的Meta Class(元类)
Meta Class
的isa指针
都指向最上层的Meta Class
,这个Meta Class
是NSObject
的Meta Class
。(包括NSObject的Meta Class
的isa指针
也是指向的NSObject
的Meta Class
)Meta Class
的super class
指针指向它本来Class
的 Super Class
的Meta Class
(这里最上层的NSObject
的Meta Class
的super class
指针仍是指向本身)NSObject Class
的super class
指向 nil
最后,特别致谢《Effective Objective-C 2.0》第二章