iOS 之动态运行时 runtime

前言:

最近研究runtime,觉得里面的东西好深,所以决定先把苹果提供的runtime.h文件简单的翻译一下,关于用法会在之后进行一些分享。

/* Types */

#if !OBJC_TYPES_DEFINED

/// An opaque type that represents a method in a class definition.
typedef struct objc_method *Method;

/// An opaque type that represents an instance variable.
typedef struct objc_ivar *Ivar;

/// An opaque type that represents a category.
typedef struct objc_category *Category;

/// An opaque type that represents an Objective-C declared property.
typedef struct objc_property *objc_property_t;

这四个type 分别是:
(类中不确定类型的一个方法)、(不确定类型的一个实例变量)、(不确定类型的一个类目)、(不确定的OC的属性声明)

struct objc_class {
    Class isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class super_class                                        OBJC2_UNAVAILABLE;
    const char *name                                         OBJC2_UNAVAILABLE;
    long version                                             OBJC2_UNAVAILABLE;
    long info                                                OBJC2_UNAVAILABLE;
    long instance_size                                       OBJC2_UNAVAILABLE;
    struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
    struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
    struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
    struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
#endif

} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */

#endif

这是定义一个objc_class 结构体;

#ifdef __OBJC__
@class Protocol;
#else
typedef struct objc_object Protocol;
#endif

/// Defines a method
struct objc_method_description {
    SEL name;               /**< The name of the method */
    char *types;            /**< The types of the method arguments */
};

/// Defines a property attribute
typedef struct {
    const char *name;           /**< The name of the attribute */
    const char *value;          /**< The value of the attribute (usually empty) */
} objc_property_attribute_t;

这段代码,我理解的就是定义的两个结构体,关于结构体,有机会写一篇内容;

/* Functions */

/* Working with Instances */

/**
 * Returns a copy of a given object.
 *
 * @param obj An Objective-C object.
 * @param size The size of the object \e obj.
 *
 * @return A copy of \e obj.
 */
OBJC_EXPORT id object_copy(id obj, size_t size)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0)
    OBJC_ARC_UNAVAILABLE;

/**
 * Frees the memory occupied by a given object.
 *
 * @param obj An Objective-C object.
 *
 * @return nil
 */
OBJC_EXPORT id object_dispose(id obj)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0)
    OBJC_ARC_UNAVAILABLE;

/**
 * Returns the class of an object.
 *
 * @param obj The object you want to inspect.
 *
 * @return The class object of which \e object is an instance,
 *  or \c Nil if \e object is \c nil.
 */
OBJC_EXPORT Class object_getClass(id obj)
     __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/**
 * Sets the class of an object.
 *
 * @param obj The object to modify.
 * @param cls A class object.
 *
 * @return The previous value of \e object‘s class, or \c Nil if \e object is \c nil.
 */
OBJC_EXPORT Class object_setClass(id obj, Class cls)
     __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/**
 * Returns whether an object is a class object.
 *
 * @param obj An Objective-C object.
 *
 * @return true if the object is a class or metaclass, false otherwise.
 */
OBJC_EXPORT BOOL object_isClass(id obj)
    __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0);

/**
 * Returns the class name of a given object.
 *
 * @param obj An Objective-C object.
 *
 * @return The name of the class of which \e obj is an instance.
 */
OBJC_EXPORT const char *object_getClassName(id obj)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

从/*Functions*/开始就是一些方法(都是实例方法):

1、id object_copy(id obj, size_t size) 返回的是传过来的对象的一个副本,需要的参数是 一个OC的对象,和对象的大小

2、id object_dispose(id obj) 释放给定的对象的内存

3、Class object_getClass(id obj) 返回的是对象的类

4、Class object_setClass(id obj, Class cls) 设置对象的类,参数是要修改的对象,一个类对象

5、BOOL object_isClass(id obj) 返回对象是否一个类的对对象

6、const char *object_getClassName(id obj) 返回给定的对象的类名(备注:类名是这个对象的实例?)

/**
 * Returns a pointer to any extra bytes allocated with an instance given object.
 *
 * @param obj An Objective-C object.
 *
 * @return A pointer to any extra bytes allocated with \e obj. If \e obj was
 *   not allocated with any extra bytes, then dereferencing the returned pointer is undefined.
 *
 * @note This function returns a pointer to any extra bytes allocated with the instance
 *  (as specified by \c class_createInstance with extraBytes>0). This memory follows the
 *  object‘s ordinary ivars, but may not be adjacent to the last ivar.
 * @note The returned pointer is guaranteed to be pointer-size aligned, even if the area following
 *  the object‘s last ivar is less aligned than that. Alignment greater than pointer-size is never
 *  guaranteed, even if the area following the object‘s last ivar is more aligned than that.
 * @note In a garbage-collected environment, the memory is scanned conservatively.
 */
OBJC_EXPORT void *object_getIndexedIvars(id obj)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0)
    OBJC_ARC_UNAVAILABLE;

/**
 * Reads the value of an instance variable in an object.
 *
 * @param obj The object containing the instance variable whose value you want to read.
 * @param ivar The Ivar describing the instance variable whose value you want to read.
 *
 * @return The value of the instance variable specified by \e ivar, or \c nil if \e object is \c nil.
 *
 * @note \c object_getIvar is faster than \c object_getInstanceVariable if the Ivar
 *  for the instance variable is already known.
 */
OBJC_EXPORT id object_getIvar(id obj, Ivar ivar)
     __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/**
 * Sets the value of an instance variable in an object.
 *
 * @param obj The object containing the instance variable whose value you want to set.
 * @param ivar The Ivar describing the instance variable whose value you want to set.
 * @param value The new value for the instance variable.
 *
 * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
 *  for the instance variable is already known.
 */
OBJC_EXPORT void object_setIvar(id obj, Ivar ivar, id value)
     __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);

/**
 * Changes the value of an instance variable of a class instance.
 *
 * @param obj A pointer to an instance of a class. Pass the object containing
 *  the instance variable whose value you wish to modify.
 * @param name A C string. Pass the name of the instance variable whose value you wish to modify.
 * @param value The new value for the instance variable.
 *
 * @return A pointer to the \c Ivar data structure that defines the type and
 *  name of the instance variable specified by \e name.
 */
OBJC_EXPORT Ivar object_setInstanceVariable(id obj, const char *name, void *value)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0)
    OBJC_ARC_UNAVAILABLE;

/**
 * Obtains the value of an instance variable of a class instance.
 *
 * @param obj A pointer to an instance of a class. Pass the object containing
 *  the instance variable whose value you wish to obtain.
 * @param name A C string. Pass the name of the instance variable whose value you wish to obtain.
 * @param outValue On return, contains a pointer to the value of the instance variable.
 *
 * @return A pointer to the \c Ivar data structure that defines the type and name of
 *  the instance variable specified by \e name.
 */
OBJC_EXPORT Ivar object_getInstanceVariable(id obj, const char *name, void **outValue)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0)
    OBJC_ARC_UNAVAILABLE;

/* Obtaining Class Definitions */

/**
 * Returns the class definition of a specified class.
 *
 * @param name The name of the class to look up.
 *
 * @return The Class object for the named class, or \c nil
 *  if the class is not registered with the Objective-C runtime.
 *
 * @note \c objc_getClass is different from \c objc_lookUpClass in that if the class
 *  is not registered, \c objc_getClass calls the class handler callback and then checks
 *  a second time to see whether the class is registered. \c objc_lookUpClass does
 *  not call the class handler callback.
 *
 * @warning Earlier implementations of this function (prior to OS X v10.0)
 *  terminate the program if the class does not exist.
 */
OBJC_EXPORT Class objc_getClass(const char *name)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);

/**
 * Returns the metaclass definition of a specified class.
 *
 * @param name The name of the class to look up.
 *
 * @return The \c Class object for the metaclass of the named class, or \c nil if the class
 *  is not registered with the Objective-C runtime.
 *
 * @note If the definition for the named class is not registered, this function calls the class handler
 *  callback and then checks a second time to see if the class is registered. However, every class
 *  definition must have a valid metaclass definition, and so the metaclass definition is always returned,
 *  whether it’s valid or not.
 */
OBJC_EXPORT Class objc_getMetaClass(const char *name)
    __OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0);
 
时间: 2024-10-14 08:04:03

iOS 之动态运行时 runtime的相关文章

iOS 之动态运行时 runtime &lt;二&gt;

/* Working with Classes */ /** * Returns the name of a class. * * @param cls A class object. * * @return The name of the class, or the empty string if \e cls is \c Nil. */ OBJC_EXPORT const char *class_getName(Class cls) __OSX_AVAILABLE_STARTING(__MA

IOS runtime动态运行时二

在C#.Java中有编译时多态和运行时多态,在OC中,只有运行时的多态,这与它的运行机制有关.OC中,方法的调用是通过消息的传递来进行的.在IOS runtime动态运行时一http://www.cnblogs.com/5ishare/p/4708647.html中主要大致介绍了下运行时的过程,这篇主要看下消息转发(实现多态的基础). 一.引入 在<objc/objc-runtime.h>中有两个.h,<objc/runtime.h>和<objc/message.h>,

iOS 运行时(runtime)浅析

本博客,直接从分类说起.都知道OC中的分类是不能直接添加属性的,意思间接是能添加属性的.那应该怎么添加呢?那就要用到运行时(runtime)机制. 一,运行时金典用法之一 现在,给HGPerson类增加一个分类:HGPerson+HG.h,给一个属性如下: @property (nonatomic, copy) NSString* name; 貌似,这样写了以后,是能调用的,但是运行就报错了. -[HGPerson setName:]: unrecognized selector sent to

iOS App 的运行时

App被启动时,从非运行状态到短暂的非激活状态,然后切换到运行状态或者后台运行状态.在启动过程中,操作系统对App创建了一个主线程来调用main方法. main方法是App的入口,用来调用UIKit框架和做一些程序运行前的预处理.XCode项目模板自动生成了mian方法,调用UIApplicationMain iOS也有自动内存管理,ARC(Automatic Refenerce Counting),@autoreleasepool中的代码的内存管理被ARC托管 App在后台运行时,会监听一些后

iOS运行时Runtime浅析

运行时是iOS中一个很重要的概念,iOS运行过程中都会被转化为runtime的C代码执行.例如[target doSomething];会被转化成objc)msgSend(target,@selector(doSomething))来执行.这篇博客会较为全面的来讲解下Runtime. OC是一门动态语言,它将很多静态语言在编译和链接时做的事放到了运行时来处理.这种动态语言的优势在于:写代码能更加灵活,可以把消息转发给想要的对象,或者随意交换一个方法的实现. OC Runtime目前有两个版本:M

iOS动态性 运行时runtime初探(强制获取并修改私有变量,强制增加及修改私有方法等)

借助前辈的力量综合一下资料. OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法.利用runtime机制让我们可以在程序运行时动态修改类.对象中的所有属性.方法,就算是私有方法以及私有属性都是可以动态修改的.本文旨在对runtime的部分特性小试牛刀,更多更全的方法可以参考系统API文件<objc/runtime.h>,demo例子可以参见CSDN的runtime高级编程系列文章. 我们出发吧! 先看一个非常平常的Father类: #import <Fou

iOS 运行时RunTime使用场景一:打点统计用户行为,深度解耦

转自:http://www.jianshu.com/p/0497afdad36d 用户统计.jpeg 用户行为统计(User Behavior Statistics, UBS)一直是移动互联网产品中必不可少的环节,也俗称埋点.在保证移动端流量不会受较大影响的前提下,PM们总是希望埋点覆盖面越广越好.目前常规的做法是将埋点代码封装成工具类,但凡工程中需要埋点(如点击事件.页面跳转)的地方都插入埋点代码.一旦项目越来越复杂,你会发现埋点的代码散落在程序的各个角落,不利于维护以及复用.本文旨在探讨利用

iOS运行时 runtime

1.运行时介绍 1. 什么是运行时 运行时机制是用 C++开发的,是一套苹果开源的框架 OC 是基于运行时开发的语言,在OC中所有的类都是运行的时候才加载. 2. OC语言 OC 是一个全动态语言,OC的一切都是基于 Runtime实现的 只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法 3. 使用场景 1. 运行时动态获取类的属性 //主要应用:字典转模型框架 MJExtension,JSONModel 2.利用关联对象为分类添加属性 3.利用交换方法拦截系统或其他框架的方法

【iOS开发-97】MediaPlayer框架中MPMoviePlayerController类视频播放的使用,运行时runtime

(1)使用媒体播放,需要先加载<MediaPlayer/MediaPlayer.h>框架,并#import导入.媒体播放需要注意的几个点在于: --全屏的设置需要在视图可见后设置才有效. --各种控制主要是通过通知NSnotificationCenter来实现的,有进入/退出全屏,播放暂停状态改变等等. #import "WPMoviePlayController.h" #import <MediaPlayer/MediaPlayer.h> @interface