zoukankan      html  css  js  c++  java
  • ocruntime

    原作: http://www.jianshu.com/p/25a319aee33d 

    三种方法的选择

    Runtime提供三种方式来将原来的方法实现代替掉,那该怎样选择它们呢?

    • Method Resolution:由于Method Resolution不能像消息转发那样可以交给其他对象来处理,所以只适用于在原来的类中代替掉。
    • Fast Forwarding:它可以将消息处理转发给其他对象,使用范围更广,不只是限于原来的对象。
    • Normal Forwarding:它跟Fast Forwarding一样可以消息转发,但它能通过NSInvocation对象获取更多消息发送的信息,例如:target、selector、arguments和返回值等信息。

    Associated Objects

    当使用Category对某个类进行扩展时,有时需要存储属性,Category是不支持的,这时需要使用Associated Objects来给已存在的类Category添加自定义的属性。Associated Objects提供三个API来向对象添加、获取和删除关联值:

    • void objc_setAssociatedObject (id object, const void *key, id value, objc_AssociationPolicy policy )
    • id objc_getAssociatedObject (id object, const void *key )
    • void objc_removeAssociatedObjects (id object )

    其中objc_AssociationPolicy是个枚举类型,它可以指定Objc内存管理的引用计数机制。

    [cpp] view plaincopy
     
    1. typedef OBJC_ENUM(uintptr_t, objc_AssociationPolicy) {  
    2.     OBJC_ASSOCIATION_ASSIGN = 0,           /**< Specifies a weak reference to the associated object. */  
    3.     OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1, /**< Specifies a strong reference to the associated object.  
    4.                                             *   The association is not made atomically. */  
    5.     OBJC_ASSOCIATION_COPY_NONATOMIC = 3,   /**< Specifies that the associated object is copied.  
    6.                                             *   The association is not made atomically. */  
    7.     OBJC_ASSOCIATION_RETAIN = 01401,       /**< Specifies a strong reference to the associated object. 
    8.                                             *   The association is made atomically. */  
    9.     OBJC_ASSOCIATION_COPY = 01403          /**< Specifies that the associated object is copied. 
    10.                                             *   The association is made atomically. */  
    11. };  

    下面有个关于NSObject+AssociatedObject Category添加属性associatedO

  • 相关阅读:
    暑假工作日志——关于51的串口的小问题
    C中怎么利用指针实现一个函数输入一个数组且输出一个数组
    2015.8.5数组与指针练习总结
    2015.8.5关于指针的个人理解
    2015.8.3keil调试遇到的问题
    2015.8.3stm32f4串口调试记录
    quartusII 错误记录
    3/4、事例三、四:透视投影(Perspective)、正射投影(Ortho)
    2、事例二: 三角形的几何变换平移(Translate)、旋转(Scale)、缩放(Rotate)
    1、事例一: 一个三角形、一个正方形(Vertex)
  • 原文地址:https://www.cnblogs.com/wcLT/p/5660533.html
Copyright © 2011-2022 走看看