源码剖析,饥馑联机版新本子

by admin on 2019年1月31日

html代码

饔飧不继数据库为你提供饥馑物品代码、物品属性介绍、物品的合成和取得格局,快来一起明白下啊!

前言

上学开发唯有两件事是值得做下来的。第一是国外人的书,第二是合法的文档。

1. 有的代码块:

<div class="layui-upload">
   <button type="button" class="layui-btn" id="cover">上传封面</button>
</div> 
<div class="layui-input-inline">
   <img id="preview" width="200px" height="200px">
</div>

点击查阅>:并日而食数据库

Runtime

一些代码块定义在措施中,用花括号'{}’包蕴。例如在method()方法中大家成立一个片段代码块。

js代码

bf88必发唯一官网 1

SEL

void method() {
    {
        int x = 22;
        System.out.println("age = " + x);
        System.out.println("这是一个局部代码块");
    }
}
var uploadInst = upload.render({
      elem:'#cover'
      ,url:'addCourse'
      ,accept:'file'  // 允许上传的文件类型
      ,auto:true // 自动上传
      ,before:function (obj) {
          console.log(obj);
          // 预览
          obj.preview(function(index,file,result) {
              // console.log(file.name);    //图片名字
              // console.log(file.type);    //图片格式
              // console.log(file.size);    //图片大小
              // console.log(result);    //图片地址
              $('#preview').attr('src',result); //图片链接 base64
          });
          // layer.load();
      }
      // 上传成功回调
      ,done:function(res) {
          // console.log(upload);
          console.log(res);

      }
      // 上传失败回调
      ,error:function(index,upload) {
          // 上传失败
      }

  });

饔飧不济联机版新本子代码大全;饥馑联机版新本子出来未来,更新了大批量的生物和物品,上边作者为我们整理了这几个新东西的代码,玩家们在控制台打入相应的代码就足以得到这几个事物了。

定义
/// An opaque type that represents a method selector.
typedef struct objc_selector *SEL;

在上述代码中,我们可以直接去掉代码块的花括号,运行效果和加代码块一致。那么,有啥样须求选择一些代码块吧?

php接口

代码应用方法

sel_getName
/// Returns the name of the method specified by a given selector.
// 通过 SEL 返回方法名字
OBJC_EXPORT const char * _Nonnull sel_getName(SEL _Nonnull sel)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

NSLog(@"--- %s", sel_getName(@selector(testFormat)));

打印:

TestProject[22016:6798169] --- testFormat

有些代码块可以限制变量的生命周期,也就是说在代码块中创立的变量只在代码块中有效,当代码块运行为止,变量就会被放出,从而省去内存空间。

 $file = request()->file('file');
 // 移动到框架应用根目录/public/uploads/ 目录下
 $info = $file->move('public/upload/');
 if ($info) {
     $path = 'public/upload/'.$info->getSaveName();
     return return_succ($path);
 }

进入游戏后按“~”键调出控制台;

sel_registerName
/** 
 * Registers a method with the Objective-C runtime system, maps the method 
 * name to a selector, and returns the selector value.
 * 
 * @param str A pointer to a C string. Pass the name of the method you wish to register.
 * 
 * @return A pointer of type SEL specifying the selector for the named method.
 * 
 * @note You must register a method name with the Objective-C runtime system to obtain the
 *  method’s selector before you can add the method to a class definition. If the method name
 *  has already been registered, this function simply returns the selector.
 */

// 注册一个方法,对这个runtime系统返回一个SEL,如果这个方法的名字早已经被注册,那么这个函数只会返回 SEL
OBJC_EXPORT SEL _Nonnull sel_registerName(const char * _Nonnull str)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

((void (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_registerName("testFormat"));

打印:

TestProject[22427:6817642] testFormat

切切实实看一下以下代码来了然一些代码块的假释变量内存空间。

bf88必发唯一官网 2

输入DebugSpawn”代码名称“ 该代码所代表的物料会冒出在鼠标所在的岗位;

object_getClassName
/** 
 * 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 * _Nonnull object_getClassName(id _Nullable obj)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
void method() {
    int age = 10;
    {
        int age = 100;        //此处报错[Duplicate local variable age];去掉前面的int类型声明即可
    }
    System.out.println("age = " + age);
}

 

输入c_give(“代码名称“,数量)物品会按所输多少出现在物品栏;

object_getIndexedIvars
/** 
 * 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 * _Nullable object_getIndexedIvars(id _Nullable obj)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
    OBJC_ARC_UNAVAILABLE;

首先在一部分代码块之外定义一个int类型的age变量。然后在一些代码块中另行定义一个int类型的age变量,此时报错说是[重新定义了三个变量age]bf88必发唯一官网,。假使大家将上述形式中的局地变量和部分代码块调换一下顺序会怎么啊?

不可指导的生物/物品则按所输多少出现在鼠标所示位置。

sel_getUid
/** 
 * Registers a method name with the Objective-C runtime system.
 * 
 * @param str A pointer to a C string. Pass the name of the method you wish to register.
 * 
 * @return A pointer of type SEL specifying the selector for the named method.
 * 
 * @note The implementation of this method is identical to the implementation of \c sel_registerName.
 * @note Prior to OS X version 10.0, this method tried to find the selector mapped to the given name
 *  and returned \c NULL if the selector was not found. This was changed for safety, because it was
 *  observed that many of the callers of this function did not check the return value for \c NULL.
 */
// 向 runtime系统根据字符串注册一个方法,返回SEL
OBJC_EXPORT SEL _Nonnull sel_getUid(const char * _Nonnull str)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

((void (*)(id, SEL))(void *)objc_msgSend)((id)self, sel_getUid("testFormat"));

打印:

TestProject[22472:6820361] testFormat
void method() {
    {
        int age = 100;
    }
    int age = 10;
    System.out.println("age = " + age);
}

bf88必发唯一官网 3

sel_isMapped
/** 
 * Identifies a selector as being valid or invalid.
 * 
 * @param sel The selector you want to identify.
 * 
 * @return YES if selector is valid and has a function implementation, NO otherwise. 
 * 
 * @warning On some platforms, an invalid reference (to invalid memory addresses) can cause
 *  a crash. 
 */
// 判断 SEL 是否存在在 SEL Dictionary
OBJC_EXPORT BOOL sel_isMapped(SEL _Nonnull sel)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

SEL sel;
if (sel_isMapped(sel)) {
        NSLog(@"有效");
}

打印:

没有打印

可以见见,那样就不会报错了,那是因为在代码块中采用完变量之后就会释放掉,所以出了代码块,内存中就不再存在age那个变量了。

绘图桌

IMP

/// A pointer to the function of a method implementation. 
#if !OBJC_OLD_DISPATCH_PROTOTYPES
typedef void (*IMP)(void /* id, SEL, ... */ ); 
#else
typedef id _Nullable (*IMP)(id _Nonnull, SEL _Nonnull, ...); 
#endif

IMP 指针指向方法的切切实实的完成

 

代码 cartographydesk

Method

2. 源码剖析,饥馑联机版新本子。布局代码块:

地图卷轴

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

结构代码块是概念在类中方法外的,同样是用花括号'{}’包括;例如:

代码 mapscroll

方法
public class Test {
    public Test() {
        System.out.println("无参构造");
    }

    public Test(int num) {
        System.out.println("带参构造");
    }

    {
        System.out.println("构造代码块");
    }

    public static void main(String[] args) {
        Test test = new Test();
        System.out.println("-------------");
        Test test2 = new Test(1);
    }
}

羽毛笔

结构
struct objc_method {
    // 方法名字
    SEL _Nonnull method_name                                 OBJC2_UNAVAILABLE;
    // 方法类型
    char * _Nullable method_types                            OBJC2_UNAVAILABLE;
   // 方法指针
    IMP _Nonnull method_imp                                  OBJC2_UNAVAILABLE;
} 

事先的代码块叫做局地代码块是因为它放在方法中,相当于是局地岗位。那么那里的结构代码块是还是不是和构造方法有着千头万绪的维系呢?

代码 featherpencil

class_replaceMethod

/** 
 * Replaces the implementation of a method for a given class.
 * 
 * @param cls The class you want to modify.
 * @param name A selector that identifies the method whose implementation you want to replace.
 * @param imp The new implementation for the method identified by name for the class identified by cls.
 * @param types An array of characters that describe the types of the arguments to the method. 
 *  Since the function must take at least two arguments—self and _cmd, the second and third characters
 *  must be “@:” (the first character is the return type).
 * 
 * @return The previous implementation of the method identified by \e name for the class identified by \e cls.
 * 
 * @note This function behaves in two different ways:
 *  - If the method identified by \e name does not yet exist, it is added as if \c class_addMethod were called. 
 *    The type encoding specified by \e types is used as given.
 *  - If the method identified by \e name does exist, its \c IMP is replaced as if \c method_setImplementation were called.
 *    The type encoding specified by \e types is ignored.
 */
// 替换给定类方法的实现,如果方法不存在则会调用class_addMethod添加方法。如果方法标识确实存在那么会调用method_setImplementation设置实现
OBJC_EXPORT IMP _Nullable
class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, 
                    const char * _Nullable types) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

由此上述代码,大家可以印证一下,运行结果如下:

月之石

class_getInstanceMethod

/** 
 * 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)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

Method methodI = class_getInstanceMethod([self class], @selector(viewWillAppear:));

打印:

(lldb) po methodI
0x000000010ca31528

[社团代码块

代码 moonrocknugget

class_getClassMethod

/** 
 * Returns a pointer to the data structure describing a given class method for a given class.
 * 
 * @param cls A pointer to a class definition. Pass the class that contains the method you want to retrieve.
 * @param name A pointer of type \c SEL. Pass the selector of the method you want to retrieve.
 * 
 * @return A pointer to the \c Method data structure that corresponds to the implementation of the 
 *  selector specified by aSelector for the class specified by aClass, or NULL if the specified 
 *  class or its superclasses do not contain an instance method with the specified selector.
 *
 * @note Note that this function searches superclasses for implementations, 
 *  whereas \c class_copyMethodList does not.
 */
 // 获取类方法
OBJC_EXPORT Method _Nullable
class_getClassMethod(Class _Nullable cls, SEL _Nonnull name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

Method method = class_getClassMethod([Person class], @selector(test));

打印:

(lldb) po method
0x0000000106c633d8

无参构造

五颜六色宝石

class_getMethodImplementation

/** 
 * Returns the function pointer that would be called if a 
 * particular message were sent to an instance of a class.
 * 
 * @param cls The class you want to inspect.
 * @param name A selector.
 * 
 * @return The function pointer that would be called if \c [object name] were called
 *  with an instance of the class, or \c NULL if \e cls is \c Nil.
 *
 * @note \c class_getMethodImplementation may be faster than \c method_getImplementation(class_getInstanceMethod(cls, name)).
 * @note The function pointer returned may be a function internal to the runtime instead of
 *  an actual method implementation. For example, if instances of the class do not respond to
 *  the selector, the function pointer returned will be part of the runtime's message forwarding machinery.
 */
 // 得到指向方法实现的指针
OBJC_EXPORT IMP _Nullable
class_getMethodImplementation(Class _Nullable cls, SEL _Nonnull name) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

IMP imp = class_getMethodImplementation([self class], @selector(viewWillAppear:));

打印:

(lldb) po imp
(UIKit`-[UIViewController viewWillAppear:])

代码 opalpreciousgem

class_getMethodImplementation_stret

/** 
 * Returns the function pointer that would be called if a particular 
 * message were sent to an instance of a class.
 * 
 * @param cls The class you want to inspect.
 * @param name A selector.
 * 
 * @return The function pointer that would be called if \c [object name] were called
 *  with an instance of the class, or \c NULL if \e cls is \c Nil.
 */
 // 返回方法的具体的实现
OBJC_EXPORT IMP _Nullable
class_getMethodImplementation_stret(Class _Nullable cls, SEL _Nonnull name) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0)
    OBJC_ARM64_UNAVAILABLE;

代码:

打印:

布局代码块

呼月者权杖

class_respondsToSelector

/** 
 * Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
 * 
 * @param cls The class you want to inspect.
 * @param sel A selector.
 * 
 * @return \c YES if instances of the class respond to the selector, otherwise \c NO.
 * 
 * @note You should usually use \c NSObject's \c respondsToSelector: or \c instancesRespondToSelector: 
 *  methods instead of this function.
 */
 // 返回一个 Boolean 判断该类是否实现了指定的方法
OBJC_EXPORT BOOL
class_respondsToSelector(Class _Nullable cls, SEL _Nonnull sel) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

BOOL isYES = class_respondsToSelector([self class], @selector(viewWillAppear:));

打印:

(lldb) po isYES
YES

带参构造]

代码 opalstaff

class_copyMethodList

/** 
 * Describes the instance methods implemented by a class.
 * 
 * @param cls The class you want to inspect.
 * @param outCount On return, contains the length of the returned array. 
 *  If outCount is NULL, the length is not returned.
 * 
 * @return An array of pointers of type Method describing the instance methods 
 *  implemented by the class—any instance methods implemented by superclasses are not included. 
 *  The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
 * 
 *  If cls implements no instance methods, or cls is Nil, returns NULL and *outCount is 0.
 * 
 * @note To get the class methods of a class, use \c class_copyMethodList(object_getClass(cls), &count).
 * @note To get the implementations of methods that may be implemented by superclasses, 
 *  use \c class_getInstanceMethod or \c class_getClassMethod.
 */
 // 得到类的方法列表
OBJC_EXPORT Method _Nonnull * _Nullable
class_copyMethodList(Class _Nullable cls, unsigned int * _Nullable outCount) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

unsigned int count;
Method *methodList = class_copyMethodList([self class], &count);
for (unsigned int i = 0; i < count; i++) {
    Method method = methodList[I];
    NSLog(@"%s", sel_getName(method_getName(method)));
}

打印:

TestProject[44285:8024690] testFormat
TestProject[44285:8024690] didReceiveMe
TestProject[44285:8024690] viewDidLoad

可以见见:无论是使用无参构造仍然有参构造来创造对象,首先都会执行协会代码块中的内容。所以它可以对目的开展开始化;即每个对象都富有的职能可以置身构造代码块中,在目的创建时,就会落到实处该功效,从而收缩代码的冗余度,进步代码复用性。

呼月基座

method_setImplementation

/** 
 * Sets the implementation of a method.
 * 
 * @param m The method for which to set an implementation.
 * @param imp The implemention to set to this method.
 * 
 * @return The previous implementation of the method.
 */
OBJC_EXPORT IMP _Nonnull
method_setImplementation(Method _Nonnull m, IMP _Nonnull imp) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Method m1 = class_getInstanceMethod([TestClass class], @selector(testLog1));
Method m2 = class_getInstanceMethod([TestClass class], @selector(testLog2));
IMP imp1 = method_getImplementation(m1);
IMP imp2 = method_getImplementation(m2);
method_setImplementation(m1, imp2);
method_setImplementation(m2, imp1);

 

代码 moonbase

method_exchangeImplementations

Method method1 = class_getInstanceMethod([NSString class], @selector(lowercaseString));
Method method2 = class_getInstanceMethod([NSString class], @selector(uppercaseString));
method_exchangeImplementations(method1, method2);//交换两个方法后,两个方法的功能相反

3. 静态代码块:

困惑的月化石

Ivar

静态代码块一样是概念在类中的,绝对于社团代码块,只是在花括号前加了一个static修饰符。同样的,大家经过代码来看一下它的成效。

鬣犬

定义
/// An opaque type that represents an instance variable.
typedef struct objc_ivar *Ivar;
public class StaticTest {
    public StaticTest() {
        System.out.println("无参构造");
    }

    public StaticTest(int num) {
        System.out.println("带参构造");
    }

    static {
        System.out.println("静态代码块");
    }

    public static void main(String[] args) {
        StaticTest st = new StaticTest();
        System.out.println("----------------");
        StaticTest st2 = new StaticTest(1);
    }
}

代码 gargoyle_houndatk

结构
struct objc_ivar {
    char * _Nullable ivar_name                               OBJC2_UNAVAILABLE;
    char * _Nullable ivar_type                               OBJC2_UNAVAILABLE;
    int ivar_offset                                          OBJC2_UNAVAILABLE;
#ifdef __LP64__
    int space                                                OBJC2_UNAVAILABLE;
#endif
}

运行结果为:

鬣犬

方法

[静态代码块

代码 gargoyle_hounddeath

object_getIvar
/** 
 * 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.
 */
1. 读取对象中,实例变量的值
OBJC_EXPORT id _Nullable
object_getIvar(id _Nullable obj, Ivar _Nonnull ivar) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
    Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
    id mantest = object_getIvar(p1, ivar);
    NSLog(@"%@", mantest);

打印:

TestProject[22563:6825947] chen test

无参构造

野猪

object_getInstanceVariable
/** 
 * 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 _Nullable
object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,
                           void * _Nullable * _Nullable outValue)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
    OBJC_ARC_UNAVAILABLE;

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");

代码 gargoyle_werepigatk

object_setInstanceVariable
/** 
 * 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.
 *
 * @note Instance variables with known memory management (such as ARC strong and weak)
 *  use that memory management. Instance variables with unknown memory management 
 *  are assigned as if they were unsafe_unretained.
 */
 //  给类动态的添加一个变量
OBJC_EXPORT Ivar _Nullable
object_setInstanceVariable(id _Nullable obj, const char * _Nonnull name,
                           void * _Nullable value)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
    OBJC_ARC_UNAVAILABLE;

代码:

Person *p1 = [[Person alloc] init];
object_setInstanceVariableWithStrongDefault(p1, "_manTest", @"shuaibi");

带参构造]

野猪

object_setIvar
/** 
 * 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 Instance variables with known memory management (such as ARC strong and weak)
 *  use that memory management. Instance variables with unknown memory management 
 *  are assigned as if they were unsafe_unretained.
 * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
 *  for the instance variable is already known.
 */
 // 可以给私有变量设置值 速度超过 object_setInstanceVariable
OBJC_EXPORT void
object_setIvar(id _Nullable obj, Ivar _Nonnull ivar, id _Nullable value) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
object_setIvar(p1, ivar, @"shuaibi");

能够看来,大家以无参和带参的花样创制对象,即使同构造代码块一样,执行在构造方法前面,不过静态代码块仅执行五遍。

代码 gargoyle_werepighowl

object_setIvarWithStrongDefault
/** 
 * 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 Instance variables with known memory management (such as ARC strong and weak)
 *  use that memory management. Instance variables with unknown memory management 
 *  are assigned as if they were strong.
 * @note \c object_setIvar is faster than \c object_setInstanceVariable if the Ivar
 *  for the instance variable is already known.
 */
 // 设置变量中实例变量的值 / iOS 10.0+
OBJC_EXPORT void
object_setIvarWithStrongDefault(id _Nullable obj, Ivar _Nonnull ivar,
                                id _Nullable value) 
    OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
if (@available(iOS 10.0, *)) {
    object_setIvarWithStrongDefault(p1, ivar, @"chen123");
} else {
    // Fallback on earlier versions
}

静态代码块是在类加载时初阶化的,随着类的加载而加载;所以有些代码必须在类型启动的时候就进行的话,须求运用静态代码块。

野猪

object_setInstanceVariableWithStrongDefault
/** 
 * 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.
 *
 * @note Instance variables with known memory management (such as ARC strong and weak)
 *  use that memory management. Instance variables with unknown memory management 
 *  are assigned as if they were strong.
 */
 // 在MRC环境下使用,更改实例的实例变量的值
OBJC_EXPORT Ivar _Nullable
object_setInstanceVariableWithStrongDefault(id _Nullable obj,
                                            const char * _Nonnull name,
                                            void * _Nullable value)
    OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0)
    OBJC_ARC_UNAVAILABLE;

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");
if (@available(iOS 10.0, *)) {
    object_setInstanceVariableWithStrongDefault(p1, "_manTest", @"shuaibi");
} else {
        // Fallback on earlier versions
}

 

代码 gargoyle_werepigdeath

object_getInstanceVariable
/** 
 * 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.
 */
 // 获取实例变量的值 MRC 环境下有用
OBJC_EXPORT Ivar _Nullable
object_getInstanceVariable(id _Nullable obj, const char * _Nonnull name,
                           void * _Nullable * _Nullable outValue)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
    OBJC_ARC_UNAVAILABLE;

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
NSLog(@" --- %@", object_getIvar(p1, ivar));

打印:

TestProject[22787:6839305]  --- chen test

4. 先行级依次:

代码 sculpture_knighthead

ivar_getName
/* Working with Instance Variables */

/** 
 * Returns the name of an instance variable.
 * 
 * @param v The instance variable you want to enquire about.
 * 
 * @return A C string containing the instance variable's name.
 */

 // 返回实时变量的名称

OBJC_EXPORT const char * _Nullable
ivar_getName(Ivar _Nonnull v) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
NSLog(@"%s", ivar_getName(ivar));

打印:

TestProject[22909:6849954] _manTest

因为有些代码块是索要调用包蕴部分代码块的法子才能博得推行,所以可以比较main()方法、静态代码块、构造代码块、构造方法之间的推行种种。

源码剖析,饥馑联机版新本子。困惑张家口石连串

ivar_getTypeEncoding
/** 
 * Returns the type string of an instance variable.
 * 
 * @param v The instance variable you want to enquire about.
 * 
 * @return A C string containing the instance variable's type encoding.
 *
 * @note For possible values, see Objective-C Runtime Programming Guide > Type Encodings.
 */
OBJC_EXPORT const char * _Nullable
ivar_getTypeEncoding(Ivar _Nonnull v) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
NSLog(@"%s", ivar_getTypeEncoding(ivar));

打印:

TestProject[23004:6857088] @"NSString"
public class YouXianJi {
    public YouXianJi() {
        System.out.println("无参构造");
    }

    public YouXianJi(int x) {
        System.out.println("带参构造");
    }

    {
        System.out.println("构造代码块");
    }

    static {
        System.out.println("静态代码块");
    }

    void method() {
        {
            System.out.println("局部代码块");
        }
    }

    public static void main(String[] args) {
        System.out.println("main()方法");
        YouXianJi yxJ = new YouXianJi(); // 无参方式创建对象
        yxJ.method();
        System.out.println("---------------------------------------");
        YouXianJi yxji = new YouXianJi(1); // 带参方式创建对象
        yxji.method();
    }
}

主教尾部

ivar_getOffset
/** 
 * Returns the offset of an instance variable.
 * 
 * @param v The instance variable you want to enquire about.
 * 
 * @return The offset of \e v.
 * 
 * @note For instance variables of type \c id or other object types, call \c object_getIvar
 *  and \c object_setIvar instead of using this offset to access the instance variable data directly.
 */
 // 得到实例变量的偏移量
OBJC_EXPORT ptrdiff_t
ivar_getOffset(Ivar _Nonnull v) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = object_getInstanceVariable(p1, "_manTest", nil);
NSLog(@"%ld", ivar_getOffset(ivar));

打印:

TestProject[23488:6907490] 8

运行结果为:

代码 sculpture_bishophead

Category

[静态代码块

铁骑底部

定义

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

main()方法

代码 sculpture_knighthead

结构

struct objc_category {
    // 分类名字
    char * _Nonnull category_name                            OBJC2_UNAVAILABLE;
    // 类名
    char * _Nonnull class_name                               OBJC2_UNAVAILABLE;
    // objc 实例方法列表
    struct objc_method_list * _Nullable instance_methods     OBJC2_UNAVAILABLE;
    // objc 类方法列表
    struct objc_method_list * _Nullable class_methods        OBJC2_UNAVAILABLE;
    // 协议列表
    struct objc_protocol_list * _Nullable protocols          OBJC2_UNAVAILABLE;
}                                                            OBJC2_UNAVAILABLE;

布局代码块

战车鼻部

objc_property_t

无参构造

代码 sculpture_rooknose

定义

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

一些代码块

安庆石水墨画连串

protocol_getProperty

/** 
 * Returns the specified property of a given protocol.
 * 
 * @param proto A protocol.
 * @param name The name of a property.
 * @param isRequiredProperty \c YES searches for a required property, \c NO searches for an optional property.
 * @param isInstanceProperty \c YES searches for an instance property, \c NO searches for a class property.
 * 
 * @return The property specified by \e name, \e isRequiredProperty, and \e isInstanceProperty for \e proto,
 *  or \c NULL if none of \e proto's properties meets the specification.
 */
 // 根据协议,属性名字返回一个属性
OBJC_EXPORT objc_property_t _Nullable
protocol_getProperty(Protocol * _Nonnull proto,
                     const char * _Nonnull name,
                     BOOL isRequiredProperty, BOOL isInstanceProperty)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Protocol *protocolA = objc_getProtocol("CatDelegate");
objc_property_t pro = protocol_getProperty(protocolA, "testMark", YES, YES);
NSLog(@"%s", property_getName(pro));

打印:

TestProject[25760:731912] testMark

主教摄影

protocol_copyPropertyList

/** 
 * Returns an array of the required instance properties declared by a protocol.
 * 
 * @note Identical to 
 * \code
 * protocol_copyPropertyList2(proto, outCount, YES, YES);
 * \endcode
 */
 // 获取一个指定协议的所有的属性,不包括方法
OBJC_EXPORT objc_property_t _Nonnull * _Nullable
protocol_copyPropertyList(Protocol * _Nonnull proto,
                          unsigned int * _Nullable outCount)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Protocol *protocolA = objc_getProtocol("CatDelegate");
unsigned int outCount;
objc_property_t *propertyList = protocol_copyPropertyList(protocolA, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
    objc_property_t property = propertyList[I];
    NSLog(@"name --- %s", property_getName(property));
}

打印:

TestProject[26269:782402] name --- testMark

社团代码块

代码 sculpture_bishopbody

protocol_copyPropertyList2

/** 
 * Returns an array of properties declared by a protocol.
 * 
 * @param proto A protocol.
 * @param outCount Upon return, contains the number of elements in the returned array.
 * @param isRequiredProperty \c YES returns required properties, \c NO returns optional properties.
 * @param isInstanceProperty \c YES returns instance properties, \c NO returns class properties.
 * 
 * @return A C array of pointers of type \c objc_property_t describing the properties declared by \e proto.
 *  Any properties declared by other protocols adopted by this protocol are not included. The array contains
 *  \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
 *  If the protocol declares no matching properties, \c NULL is returned and \c *outCount is \c 0.
 */
 // 获取协议的所有的属性,分为required 和 optional。
 这个有点鸡肋我试了 optional 不起作用
OBJC_EXPORT objc_property_t _Nonnull * _Nullable
protocol_copyPropertyList2(Protocol * _Nonnull proto,
                           unsigned int * _Nullable outCount,
                           BOOL isRequiredProperty, BOOL isInstanceProperty)
    OBJC_AVAILABLE(10.12, 10.0, 10.0, 3.0, 2.0);

代码:

Protocol *protocolA = objc_getProtocol("CatDelegate");
unsigned int outCount;
if (@available(iOS 10.0, *)) {
    objc_property_t *propertyList = protocol_copyPropertyList2(protocolA, &outCount, NO, YES);
    for (unsigned int i = 0; i < outCount; i++) {
        objc_property_t property = propertyList[I];
        NSLog(@"name --- %s", property_getName(property));
    }
} else {
    // Fallback on earlier versions
}

打印:

TestProject[26895:821115] name --- optionalMark
TestProject[26895:821115] name --- testMark

带参构造

骑兵摄影

objc_class

部分代码块]

代码 sculpture_knightbody

结构

struct objc_class {
    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;

#if !__OBJC2__
    Class _Nullable super_class                              OBJC2_UNAVAILABLE;
    const char * _Nonnull name                               OBJC2_UNAVAILABLE;
    long version                                             OBJC2_UNAVAILABLE;
    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;

从而能够看出来优先实施各种是静态代码块 > main()方法 >
构造代码块 > 构造方法。

战车壁画

object_copy

/** 
 * 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.
 */
// 对象的 copy 属于深 copy
OBJC_EXPORT id _Nullable object_copy(id _Nullable obj, size_t size)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
    OBJC_ARC_UNAVAILABLE;

代码:

NSLog(@"self --- %p", self);
NSLog(@"object_copy --- %p", object_copy(self, sizeof(self)));

打印:

TestProject[23793:6934176] self --- 0x7f9f8c4049e0
TestProject[23793:6934176] object_copy --- 0x7f9f8c70a1d0

 

代码 sculpture_rookbody

object_dispose

/** 
 * Frees the memory occupied by a given object.
 * 
 * @param obj An Objective-C object.
 * 
 * @return nil
 */
 // 释放给定对象占用的内存
OBJC_EXPORT id _Nullable
object_dispose(id _Nullable obj)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0)
    OBJC_ARC_UNAVAILABLE;

代码:

NSLog(@"%@",object_dispose(p1));

打印:

(lldb) po p1
<Person: 0x60000000ec50>

2018-03-09 17:15:38.690 YZTestProject[24032:6953790] (null)
(lldb) po p1
0x000060000000ec50

接下来大家再看一下有着持续关系的子类和父类之间的静态代码块、构造代码块、构造方法之间的实施各种。

bf88必发唯一官网 4

object_getClass

/** 
 * 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 _Nullable
object_getClass(id _Nullable obj) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
NSLog(@"%@", object_getClass(p1));

打印:

TestProject[24120:6960612] Person
class Father {

    public Father() {
        System.out.println("父类无参构造");
    }

    Father(int num) {
        System.out.println("父类带参构造");
    }

    {
        System.out.println("父类构造代码块");
    }

    static {
        System.out.println("父类静态代码块");
    }
}

class Son extends Father {

    public Son() {
        System.out.println("子类无参构造");
    }

    public Son(int num) {
        System.out.println("子类带参构造");
    }

    {
        System.out.println("子类构造代码块");
    }

    static {
        System.out.println("子类静态代码块");
    }
}

public class Init {

    public static void main(String[] args) {
        Son son = new Son();
        Son son2 = new Son(1);
        System.out.println("-------------------------");
        Father father = new Father();
        Father father2 = new Father(1);
        System.out.println("-------------------------");
        Father father3 = new Son();
        Father father4 = new Son(1);
    }
}

蘑菇蛤/毒蕈

object_setClass
/** 
 * 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 _Nullable
object_setClass(id _Nullable obj, Class _Nonnull cls) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
NSLog(@"\n p1 = %@", p1);
Class c1 = object_setClass(p1, [Cat class]);
NSLog(@"\n c1 = %@ \n p1 = %@", c1, p1);

打印:

TestProject[24490:6987169] 
 p1 = <Person: 0x60000000a580>
TestProject[24490:6987169] 
 c1 = Person 
 p1 = <Cat: 0x60000000a580>

运作结果如下:

代码 toadstool

object_isClass
/** 
 * 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 _Nullable obj)
    OBJC_AVAILABLE(10.10, 8.0, 9.0, 1.0, 2.0);

代码:

BOOL isp1 = object_isClass([p1 class]);
Person * p2;
BOOL isp2 = object_isClass([p2 class]);
NSString * p3 = @"test";
BOOL isp3 = object_isClass([p3 class]);
NSLog(@"%i", isp1);
NSLog(@"%i", isp2);
NSLog(@"%i", isp3);

打印:

TestProject[24619:6998034] 1
TestProject[24619:6998034] 0
TestProject[24619:6998034] 1

[父类静态代码块

毒蕈树菇

objc_getClass
/* 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 _Nullable
objc_getClass(const char * _Nonnull name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

Cat *cat1 = [[Cat alloc] init];
Class c1 = object_getClass(cat1);
NSLog(@"%@", c1);
NSLog(@"%@", cat1.name);

打印:

TestProject[24927:7020395] Cat
TestProject[24927:7020395] testH

子类静态代码块

代码 mushroomsprout

objc_getMetaClass
/** 
 * 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 _Nullable
objc_getMetaClass(const char * _Nonnull name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

Cat *cat1 = [[Cat alloc] init];
Class c1 = objc_getMetaClass("Cat");
NSLog(@"%@", c1);
NSLog(@"%@", cat1.name);

打印:

TestProject[25712:7077004] Cat
TestProject[25712:7077004] testH

父类构造代码块

毒蕈皮

objc_lookUpClass
/** 
 * 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 this function 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. This function does not call the class handler callback.
 */
 // 获取指定的类,如果没有注册则返回 nil
OBJC_EXPORT Class _Nullable
objc_lookUpClass(const char * _Nonnull name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

Class cl1 = objc_lookUpClass("Ca");
Class cl2 = objc_lookUpClass("Cat");

父类无参构造

代码 shroom_skin

objc_getRequiredClass
/** 
 * 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.
 * 
 * @note This function is the same as \c objc_getClass, but kills the process if the class is not found.
 * @note This function is used by ZeroLink, where failing to find a class would be a compile-time link error without ZeroLink.
 */
 // 获取指定类,如果这个类不存在则崩溃
OBJC_EXPORT Class _Nonnull
objc_getRequiredClass(const char * _Nonnull name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

Class cl1 = objc_getRequiredClass("Ca");

子类构造代码块

蘑菇灯

objc_getClassList
/** 
 * Obtains the list of registered class definitions.
 * 
 * @param buffer An array of \c Class values. On output, each \c Class value points to
 *  one class definition, up to either \e bufferCount or the total number of registered classes,
 *  whichever is less. You can pass \c NULL to obtain the total number of registered class
 *  definitions without actually retrieving any class definitions.
 * @param bufferCount An integer value. Pass the number of pointers for which you have allocated space
 *  in \e buffer. On return, this function fills in only this number of elements. If this number is less
 *  than the number of registered classes, this function returns an arbitrary subset of the registered classes.
 * 
 * @return An integer value indicating the total number of registered classes.
 * 
 * @note The Objective-C runtime library automatically registers all the classes defined in your source code.
 *  You can create class definitions at runtime and register them with the \c objc_addClass function.
 * 
 * @warning You cannot assume that class objects you get from this function are classes that inherit from \c NSObject,
 *  so you cannot safely call any methods on such classes without detecting that the method is implemented first.
 */
 // 获取所有已经注册的类
OBJC_EXPORT int
objc_getClassList(Class _Nonnull * _Nullable buffer, int bufferCount)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

Class *bufferClass;
int numClasses;
numClasses = objc_getClassList(NULL, 0);
if(numClasses > 0) {
    bufferClass = (Class *)malloc(sizeof(Class)*numClasses);
    numClasses = objc_getClassList(bufferClass, numClasses);
    NSLog(@"numer of classes: %d", numClasses);
    for (int i = 0; i < numClasses; i++) {
        Class cls = bufferClass[i];
        NSLog(@"class name: %s", class_getName(cls));
    }
    free(bufferClass);
}

输出结果:

TestProject[41171:7767871] numer of classes: 4739

子类无参构造

代码 mushroom_light

objc_copyClassList
/** 
 * Creates and returns a list of pointers to all registered class definitions.
 * 
 * @param outCount An integer pointer used to store the number of classes returned by
 *  this function in the list. It can be \c nil.
 * 
 * @return A nil terminated array of classes. It must be freed with \c free().
 * 
 * @see objc_getClassList
 */
 // 获取所有已经注册的类
OBJC_EXPORT Class _Nonnull * _Nullable
objc_copyClassList(unsigned int * _Nullable outCount)
    OBJC_AVAILABLE(10.7, 3.1, 9.0, 1.0, 2.0);

代码:

unsigned int outCount;
Class *classes = objc_copyClassList(&outCount);
for (int i = 0; i < outCount; i++) {
    NSLog(@"%s", class_getName(classes[i]));
}

打印:

TestProject[41322:7782252] JSExport
TestProject[41322:7782252] NSLeafProxy
TestProject[41322:7782252] NSProxy
TestProject[41322:7782252] _UITargetedProxy
TestProject[41322:7782252] _UIViewServiceReplyControlTrampoline

父类构造代码块

多彩蘑菇灯

class_getName
/* 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 * _Nonnull
class_getName(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

NSLog(@"%s", class_getName([self class]));

打印:

TestProject[41376:7786142] ViewController

父类无参构造

代码 mushroom_light2

class_isMetaClass
/** 
 * Returns a Boolean value that indicates whether a class object is a metaclass.
 * 
 * @param cls A class object.
 * 
 * @return \c YES if \e cls is a metaclass, \c NO if \e cls is a non-meta class, 
 *  \c NO if \e cls is \c Nil.
 */
 // 判断输出的类是否为元类
OBJC_EXPORT BOOL
class_isMetaClass(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

if (class_isMetaClass(objc_getMetaClass("Cat"))) {
    NSLog(@"YES");
}

打印:

TestProject[41512:7792603] YES

子类构造代码块

蘑菇帽种类

class_getSuperclass
/** 
 * Returns the superclass of a class.
 * 
 * @param cls A class object.
 * 
 * @return The superclass of the class, or \c Nil if
 *  \e cls is a root class, or \c Nil if \e cls is \c Nil.
 *
 * @note You should usually use \c NSObject's \c superclass method instead of this function.
 */
 // 得到这个类的父类
OBJC_EXPORT Class _Nullable
class_getSuperclass(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

NSLog(@"%@", class_getSuperclass([self class]));

打印:

TestProject[41571:7797664] UIViewController

子类带参构造

绿蘑菇帽

class_setSuperclass
/** 
 * Sets the superclass of a given class.
 * 
 * @param cls The class whose superclass you want to set.
 * @param newSuper The new superclass for cls.
 * 
 * @return The old superclass for cls.
 * 
 * @warning You should not use this function.
 */
 // 给类设置新的父类
OBJC_EXPORT Class _Nonnull
class_setSuperclass(Class _Nonnull cls, Class _Nonnull newSuper) 
    __OSX_DEPRECATED(10.5, 10.5, "not recommended") 
    __IOS_DEPRECATED(2.0, 2.0, "not recommended") 
    __TVOS_DEPRECATED(9.0, 9.0, "not recommended") 
    __WATCHOS_DEPRECATED(1.0, 1.0, "not recommended")
    __BRIDGEOS_DEPRECATED(2.0, 2.0, "not recommended");

代码:

NSLog(@"%@", class_getSuperclass(NSClassFromString(@"ViewController")));
    class_setSuperclass(NSClassFromString(@"ViewController"), NSClassFromString(@"UIImageView"));
    NSLog(@"%@", class_getSuperclass(NSClassFromString(@"ViewController")));

打印:

TestProject[41901:7839232] UIViewController
TestProject[41901:7839232] UIImageView

代码 green_mushroomhat

class_getVersion
/** 
 * Returns the version number of a class definition.
 * 
 * @param cls A pointer to a \c Class data structure. Pass
 *  the class definition for which you wish to obtain the version.
 * 
 * @return An integer indicating the version number of the class definition.
 *
 * @see class_setVersion
 */
 // 获取类的版本号  待续...
OBJC_EXPORT int
class_getVersion(Class _Nullable cls)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

NSLog(@"%d", class_getVersion([self class]));

打印:

TestProject[42051:7849333] 0

父类构造代码块

蓝蘑菇帽

class_setVersion
/** 
 * Sets the version number of a class definition.
 * 
 * @param cls A pointer to an Class data structure. 
 *  Pass the class definition for which you wish to set the version.
 * @param version An integer. Pass the new version number of the class definition.
 *
 * @note You can use the version number of the class definition to provide versioning of the
 *  interface that your class represents to other classes. This is especially useful for object
 *  serialization (that is, archiving of the object in a flattened form), where it is important to
 *  recognize changes to the layout of the instance variables in different class-definition versions.
 * @note Classes derived from the Foundation framework \c NSObject class can set the class-definition
 *  version number using the \c setVersion: class method, which is implemented using the \c class_setVersion function.
 */
 // 设置类的版本号
OBJC_EXPORT void
class_setVersion(Class _Nullable cls, int version)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

NSLog(@"%d", class_getVersion([self class]));
class_setVersion([self class], 2);
NSLog(@"%d", class_getVersion([self class]));

版本:

TestProject[42247:7871127] 0
TestProject[42247:7871127] 2

父类无参构造

代码 blue_mushroomhat

class_getInstanceSize
/** 
 * Returns the size of instances of a class.
 * 
 * @param cls A class object.
 * 
 * @return The size in bytes of instances of the class \e cls, or \c 0 if \e cls is \c Nil.
 */
 // 返回实例类的大小,以字节为单位
OBJC_EXPORT size_t
class_getInstanceSize(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

NSLog(@"%ld", class_getInstanceSize([self class]));

打印:

TestProject[42305:7873713] 760

父类构造代码块

红蘑菇帽

class_getInstanceVariable
/** 
 * Returns the \c Ivar for a specified instance variable of a given class.
 * 
 * @param cls The class whose instance variable you wish to obtain.
 * @param name The name of the instance variable definition to obtain.
 * 
 * @return A pointer to an \c Ivar data structure containing information about 
 *  the instance variable specified by \e name.
 */
OBJC_EXPORT Ivar _Nullable
class_getInstanceVariable(Class _Nullable cls, const char * _Nonnull name)
    OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);

代码:

Person *p1 = [[Person alloc] init];
Ivar ivar = class_getInstanceVariable([p1 class], "_manTest");

打印:

(lldb) po ivar
0x00000001098a1460

父类带参构造

代码 red_mushroomhat

class_getClassVariable
/** 
 * Returns the Ivar for a specified class variable of a given class.
 * 
 * @param cls The class definition whose class variable you wish to obtain.
 * @param name The name of the class variable definition to obtain.
 * 
 * @return A pointer to an \c Ivar data structure containing information about the class variable specified by \e name.
 */
 // 获取参数 cls 类的变量名为name的变量

OBJC_EXPORT Ivar _Nullable
class_getClassVariable(Class _Nullable cls, const char * _Nonnull name) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Ivar viewIvar = class_getClassVariable([UIView class], "isa");
id getViewIvar = object_getIvar([UIView class], viewIvar);
NSLog(@"%d", class_isMetaClass(getViewIvar));
NSLog(@"%@", getViewIvar);

打印:

TestProject[42676:7900606] 1
TestProject[42676:7900606] UIView

和睦相处的稻草人

class_copyIvarList
/** 
 * Describes the instance variables declared by a class.
 * 
 * @param cls The class to inspect.
 * @param outCount On return, contains the length of the returned array. 
 *  If outCount is NULL, the length is not returned.
 * 
 * @return An array of pointers of type Ivar describing the instance variables declared by the class. 
 *  Any instance variables declared by superclasses are not included. The array contains *outCount 
 *  pointers followed by a NULL terminator. You must free the array with free().
 * 
 *  If the class declares no instance variables, or cls is Nil, NULL is returned and *outCount is 0.
 */
 // 一个 Ivar 类型的指针数组,用于描述该类声明的实例变量,必须用free释放该数组
OBJC_EXPORT Ivar _Nonnull * _Nullable
class_copyIvarList(Class _Nullable cls, unsigned int * _Nullable outCount) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

unsigned int count;
Ivar *ivars = class_copyIvarList([Person class], &count);
for (unsigned int i = 0; i < count; i++) {
    Ivar ivar = ivars[I];
    const char *name = ivar_getName(ivar);
    const char *type = ivar_getTypeEncoding(ivar);
    NSLog(@"类型为%s 的 %s", type, name);

}
free(ivars);

打印:

TestProject[42937:7913951] 类型为@"NSString" 的 _manTest

父类构造代码块

代码 scarecrow

Protocol

定义:

typedef struct objc_object Protocol;

父类无参构造

金丝雀

class_conformsToProtocol

/** 
 * Returns a Boolean value that indicates whether a class conforms to a given protocol.
 * 
 * @param cls The class you want to inspect.
 * @param protocol A protocol.
 *
 * @return YES if cls conforms to protocol, otherwise NO.
 *
 * @note You should usually use NSObject's conformsToProtocol: method instead of this function.
 */
// 返回一个 Boolean 值,检查某个类是否遵从某个协议
OBJC_EXPORT BOOL
class_conformsToProtocol(Class _Nullable cls, Protocol * _Nullable protocol) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Protocol *protocol = NSProtocolFromString(@"UITableViewDataSource");
BOOL isConforms = class_conformsToProtocol([UITableViewController class], protocol);

打印:

(lldb) po isConforms
YES

子类构造代码块

代码 canary

class_copyProtocolList

/** 
 * Describes the protocols adopted by a class.
 * 
 * @param cls The class you want to inspect.
 * @param outCount On return, contains the length of the returned array. 
 *  If outCount is NULL, the length is not returned.
 * 
 * @return An array of pointers of type Protocol* describing the protocols adopted 
 *  by the class. Any protocols adopted by superclasses or other protocols are not included. 
 *  The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().
 * 
 *  If cls adopts no protocols, or cls is Nil, returns NULL and *outCount is 0.
 */
 // 得到一个类遵守协议的个数
OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable 
class_copyProtocolList(Class _Nullable cls, unsigned int * _Nullable outCount)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

unsigned int count;
Protocol **protocolList = class_copyProtocolList([UITableViewController class], &count);
for (unsigned int i = 0; i < count; i++) {
    Protocol *pro = protocolList[I];
    NSLog(@"%s", protocol_getName(pro));
}

打印:

TestProject[19640:180921] _UIKeyboardAutoRespondingScrollViewController
TestProject[19640:180921] UITableViewFocusDelegateLegacy
TestProject[19640:180921] UIViewControllerPreviewingDelegate
TestProject[19640:180921] UIViewControllerPreviewingDelegate_Deprecated
TestProject[19640:180921] UITableViewDelegate
TestProject[19640:180921] UITableViewDataSource

子类无参构造

金色羽毛

objc_getProtocol

/* Working with Protocols */

/** 
 * Returns a specified protocol.
 * 
 * @param name The name of a protocol.
 * 
 * @return The protocol named \e name, or \c NULL if no protocol named \e name could be found.
 * 
 * @note This function acquires the runtime lock.
 */
 // 获取名称为 name 的协议
OBJC_EXPORT Protocol * _Nullable
objc_getProtocol(const char * _Nonnull name)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Protocol *protocol = objc_getProtocol("UITableViewDataSource");

打印:

(lldb) po protocol
<Protocol: 0x107dbf5e0>

父类构造代码块

代码 feather_canary

objc_copyProtocolList

/** 
 * Returns an array of all the protocols known to the runtime.
 * 
 * @param outCount Upon return, contains the number of protocols in the returned array.
 * 
 * @return A C array of all the protocols known to the runtime. The array contains \c *outCount
 *  pointers followed by a \c NULL terminator. You must free the list with \c free().
 * 
 * @note This function acquires the runtime lock.
 */
 // 返回运行时系统所有的协议
OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
objc_copyProtocolList(unsigned int * _Nullable outCount)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

unsigned int outCount;
Protocol **protocolList = objc_copyProtocolList(&outCount);
for (unsigned int i = 0; i < outCount; i++) {
    Protocol *protocol = protocolList[I];
    NSLog(@"%s", protocol_getName(protocol));
}

打印:

TestProject[23401:517412] UIVideoEditorControllerDelegate
TestProject[23401:517412] _UIIVCResponseDelegate
TestProject[23401:517412] _UITouchable
TestProject[23401:517412] WebOpenPanelResultListener
TestProject[23401:517412] SBSRemoteAlertClientHandle
TestProject[23401:517412] _UIContentContainerInternal
TestProject[23401:517412] _UIKeyboardAutoRespondingScrollView
TestProject[23401:517412] CAMLWriterDelegate
TestProject[23401:517412] OS_nw_endpoint_resolver
TestProject[23401:517412] _UIActivityHelperDelegate

父类无参构造

雷电吹箭

protocol_conformsToProtocol

/** 
 * Returns a Boolean value that indicates whether one protocol conforms to another protocol.
 * 
 * @param proto A protocol.
 * @param other A protocol.
 * 
 * @return \c YES if \e proto conforms to \e other, otherwise \c NO.
 * 
 * @note One protocol can incorporate other protocols using the same syntax 
 *  that classes use to adopt a protocol:
 *  \code
 *  @protocol ProtocolName < protocol list >
 *  \endcode
 *  All the protocols listed between angle brackets are considered part of the ProtocolName protocol.
 */
 // 判断一个协议是否遵从了另外一个协议,第一个协议是否遵守了第二个协议
OBJC_EXPORT BOOL
protocol_conformsToProtocol(Protocol * _Nullable proto,
                            Protocol * _Nullable other)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Protocol *tableViewDelegate = objc_getProtocol("UITableViewDelegate");
Protocol *scrollViewDelegate = objc_getProtocol("UIScrollViewDelegate");
BOOL isConform = protocol_conformsToProtocol(tableViewDelegate, scrollViewDelegate);

打印:

(lldb) po isConform
YES
点进去,可以看到第一个协议确实遵守了第二个协议

子类构造代码块

代码 blowdart_yellow

protocol_isEqual

/** 
 * Returns a Boolean value that indicates whether two protocols are equal.
 * 
 * @param proto A protocol.
 * @param other A protocol.
 * 
 * @return \c YES if \e proto is the same as \e other, otherwise \c NO.
 */
 // 判断两个协议是否相等
OBJC_EXPORT BOOL
protocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Protocol *protocolA = objc_getProtocol("UITableViewDataSource");
Protocol *protocolB = NSProtocolFromString(@"UITableViewDataSource");
BOOL isEqual = protocol_isEqual(protocolA, protocolB);

打印:

(lldb) po isEqual
YES

子类带参构造]

龙鳞火炉

protocol_getName

/** 
 * Returns the name of a protocol.
 * 
 * @param p A protocol.
 * 
 * @return The name of the protocol \e p as a C string.
 */
 根据协议得到协议的名称
OBJC_EXPORT const char * _Nonnull
protocol_getName(Protocol * _Nonnull proto)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Protocol *protocolA = objc_getProtocol("UITableViewDataSource");
NSLog(@"%s", protocol_getName(protocolA));

打印:

TestProject[23900:551699] UITableViewDataSource

从而优先级是父类静态代码块 > 子类静态代码块 > 父类构造代码块 >
父类构造方法 >
子类协会代码块 > 子类构造方法。

代码 dragonflyfurnace

protocol_copyProtocolList

/** 
 * Returns an array of the protocols adopted by a protocol.
 * 
 * @param proto A protocol.
 * @param outCount Upon return, contains the number of elements in the returned array.
 * 
 * @return A C array of protocols adopted by \e proto. The array contains \e *outCount pointers
 *  followed by a \c NULL terminator. You must free the array with \c free().
 *  If the protocol declares no properties, \c NULL is returned and \c *outCount is \c 0.
 */
 // 返回协议所遵守的协议,以数组的形式展现
OBJC_EXPORT Protocol * __unsafe_unretained _Nonnull * _Nullable
protocol_copyProtocolList(Protocol * _Nonnull proto,
                          unsigned int * _Nullable outCount)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

Protocol *protocolA = objc_getProtocol("CatDelegate");
unsigned int outCount;
Protocol **protocolList = protocol_copyProtocolList(protocolA, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
    Protocol *protocol = protocolList[I];
    NSLog(@"name --- %s", protocol_getName(protocol));
}

打印:

TestProject[26997:828030] name --- NSObject

阴影战车

objc_allocateProtocol

/** 
 * Creates a new protocol instance that cannot be used until registered with
 * \c objc_registerProtocol()
 * 
 * @param name The name of the protocol to create.
 *
 * @return The Protocol instance on success, \c nil if a protocol
 *  with the same name already exists. 
 * @note There is no dispose method for this. 
 */
 // 创建一个新的协议实例,但是需要通过 objc_registerProtocol 注册使用
OBJC_EXPORT Protocol * _Nullable
objc_allocateProtocol(const char * _Nonnull name) 
    OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);

代码:

Protocol *protocol = objc_allocateProtocol("PersonDelegate");

打印:

(lldb) po protocol
<__IncompleteProtocol: 0x6080000a84c0>

代码 shadow_rook

objc_registerProtocol

/** 
 * Registers a newly constructed protocol with the runtime. The protocol
 * will be ready for use and is immutable after this.
 * 
 * @param proto The protocol you want to register.
 */
 // 注册协议
OBJC_EXPORT void
objc_registerProtocol(Protocol * _Nonnull proto) 
    OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);

代码:

Protocol *protocol = objc_allocateProtocol("PersonDelegate");
// 声明这个协议还是不可以用的,还是要注册这个协议的
objc_registerProtocol(protocol);

打印:

(lldb) po protocol
<Protocol: 0x6080000a84c0>

黑影主教

protocol_addMethodDescription

/** 
 * Adds a method to a protocol. The protocol must be under construction.
 * 
 * @param proto The protocol to add a method to.
 * @param name The name of the method to add.
 * @param types A C string that represents the method signature.
 * @param isRequiredMethod YES if the method is not an optional method.
 * @param isInstanceMethod YES if the method is an instance method. 
 */
 // 给没有创建的协议添加指定的方法,已经注册的协议无法添加
OBJC_EXPORT void
protocol_addMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull name,
                              const char * _Nullable types,
                              BOOL isRequiredMethod, BOOL isInstanceMethod) 
    OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);

代码:

Protocol *protocolB = objc_allocateProtocol("PersonDelegate");
protocol_addMethodDescription(protocolB, @selector(runtimeTestAction3), "v@:", NO, YES);
unsigned int outCount;
struct objc_method_description *descList = protocol_copyMethodDescriptionList(protocolB, NO, YES, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
    struct objc_method_description desc = descList[I];
    char *types = desc.types;
    NSLog(@"name: %@, type: %s", NSStringFromSelector(desc.name), types);
}

打印:

TestProject[29062:1027253] name: runtimeTestAction3, type: v@:

代码 shadow_bishop

protocol_addProtocol

/** 
 * Adds an incorporated protocol to another protocol. The protocol being
 * added to must still be under construction, while the additional protocol
 * must be already constructed.
 * 
 * @param proto The protocol you want to add to, it must be under construction.
 * @param addition The protocol you want to incorporate into \e proto, it must be registered.
 */
 // 添加一个已注册的协议到未注册的协议当中
 proto:未注册的协议
 addition:被添加的已注册协议

OBJC_EXPORT void
protocol_addProtocol(Protocol * _Nonnull proto, Protocol * _Nonnull addition) 
    OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);

代码:

// 声明一个协议返回协议的实例
Protocol *protocol = objc_allocateProtocol("ProjectDelegate");
objc_registerProtocol(protocol);   // 协议方法已经注册

Protocol *testProtocol = objc_allocateProtocol("TestProtocol");
protocol_addProtocol(testProtocol, protocol);

// 打印这个协议遵循的协议组
unsigned int count;
Protocol **protocolList = protocol_copyProtocolList(testProtocol, &count);
for (unsigned int i = 0; i < count; i++) {
    Protocol *protocolC = protocolList[I];
    NSLog(@"name --- %s", protocol_getName(protocolC));
}

打印:

TestProject[32945:1206976] name --- ProjectDelegate

影子骑士

protocol_addProperty

/** 
 * Adds a property to a protocol. The protocol must be under construction. 
 * 
 * @param proto The protocol to add a property to.
 * @param name The name of the property.
 * @param attributes An array of property attributes.
 * @param attributeCount The number of attributes in \e attributes.
 * @param isRequiredProperty YES if the property (accessor methods) is not optional. 
 * @param isInstanceProperty YES if the property (accessor methods) are instance methods. 
 *  This is the only case allowed fo a property, as a result, setting this to NO will 
 *  not add the property to the protocol at all. 
 */
 //
OBJC_EXPORT void
protocol_addProperty(Protocol * _Nonnull proto, const char * _Nonnull name,
                     const objc_property_attribute_t * _Nullable attributes,
                     unsigned int attributeCount,
                     BOOL isRequiredProperty, BOOL isInstanceProperty)
    OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);

代码:

    // 创建新的协议
    Protocol *newProtocol = objc_allocateProtocol("DataDelegate");

    // 给一个协议添加一个属性
    objc_property_attribute_t attr_T = {"T", "@\"NSString\""};   // @encod
    objc_property_attribute_t attr_N = {"N", ""};  // 原子性
    objc_property_attribute_t attr_C = {"C", ""};  // copy
    objc_property_attribute_t attr_V = {"V", "_age"}; // 实例变量
    objc_property_attribute_t attrs[] = {attr_T, attr_N, attr_C, attr_V};
    protocol_addProperty(newProtocol, "age", attrs, 4, YES, YES);

    // 打印协议的属性
    unsigned int outCount;
    objc_property_t *propertyList = protocol_copyPropertyList(newProtocol, &outCount);
    for (unsigned int i = 0; i < outCount; i++) {
        objc_property_t property = propertyList[I];
        unsigned propertyCount;
        objc_property_attribute_t *attrList = property_copyAttributeList(property, &propertyCount);
        for (unsigned int j  = 0; j < propertyCount; j++) {
            objc_property_attribute_t attr = attrList[j];
            const char *name = attr.name;
            const char *value = attr.value;
            NSLog(@"name: %s, value: %s", name, value);
        }
    }

打印:

TestProject[33846:1268389] name: T, value: @"NSString"
TestProject[33846:1268389] name: N, value: 
TestProject[33846:1268389] name: C, value: 
TestProject[33846:1268389] name: V, value: _age

代码 shadow_knight

殊不知的化石骨架

objc_method_description

结构:

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

代码 fossil_stalker

protocol_getMethodDescription

/** 
 * Returns a method description structure for a specified method of a given protocol.
 * 
 * @param p A protocol.
 * @param aSel A selector.
 * @param isRequiredMethod A Boolean value that indicates whether aSel is a required method.
 * @param isInstanceMethod A Boolean value that indicates whether aSel is an instance method.
 * 
 * @return An \c objc_method_description structure that describes the method specified by \e aSel,
 *  \e isRequiredMethod, and \e isInstanceMethod for the protocol \e p.
 *  If the protocol does not contain the specified method, returns an \c objc_method_description structure
 *  with the value \c {NULL, \c NULL}.
 * 
 * @note This function recursively searches any protocols that this protocol conforms to.
 */
 // 返回协议中指定方法的描述
OBJC_EXPORT struct objc_method_description
protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel,
                              BOOL isRequiredMethod, BOOL isInstanceMethod)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

struct objc_method_description desc = protocol_getMethodDescription(protocolA, @selector(numberOfSectionsInTableView:), NO, YES);

打印:

(lldb) po desc.name
"numberOfSectionsInTableView:"

(lldb) po desc.types
"q24@0:8@16"

化石碎片

protocol_copyMethodDescriptionList

/** 
 * Returns an array of method descriptions of methods meeting a given specification for a given protocol.
 * 
 * @param p A protocol.
 * @param isRequiredMethod A Boolean value that indicates whether returned methods should
 *  be required methods (pass YES to specify required methods).
 * @param isInstanceMethod A Boolean value that indicates whether returned methods should
 *  be instance methods (pass YES to specify instance methods).
 * @param outCount Upon return, contains the number of method description structures in the returned array.
 * 
 * @return A C array of \c objc_method_description structures containing the names and types of \e p's methods 
 *  specified by \e isRequiredMethod and \e isInstanceMethod. The array contains \c *outCount pointers followed
 *  by a \c NULL terminator. You must free the list with \c free().
 *  If the protocol declares no methods that meet the specification, \c NULL is returned and \c *outCount is 0.
 * 
 * @note Methods in other protocols adopted by this protocol are not included.
 */
 // 得到方法描述列表
OBJC_EXPORT struct objc_method_description * _Nullable
protocol_copyMethodDescriptionList(Protocol * _Nonnull proto,
                                   BOOL isRequiredMethod,
                                   BOOL isInstanceMethod,
                                   unsigned int * _Nullable outCount)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

unsigned int outCount;
struct objc_method_description *descList = protocol_copyMethodDescriptionList(protocolA, YES, YES, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
    struct objc_method_description desc = descList[I];
    char *types = desc.types;
    NSLog(@"name: %@, type: %s", NSStringFromSelector(desc.name), types);
}

打印:

TestProject[25150:681145] name: tableView:numberOfRowsInSection:, type: q32@0:8@16q24
TestProject[25150:681145] name: tableView:cellForRowAtIndexPath:, type: @32@0:8@16@24

代码 fossil_piece

objc_property_t

定义:

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

阴影之心

class_getProperty

/** 
 * Returns a property with a given name of a given class.
 * 
 * @param cls The class you want to inspect.
 * @param name The name of the property you want to inspect.
 * 
 * @return A pointer of type \c objc_property_t describing the property, or
 *  \c NULL if the class does not declare a property with that name, 
 *  or \c NULL if \e cls is \c Nil.
 */
 // 返回一个属性,通过参数类,和名字
OBJC_EXPORT objc_property_t _Nullable
class_getProperty(Class _Nullable cls, const char * _Nonnull name)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

objc_property_t property_t = class_getProperty([Person class], "age");
const char *name = property_getName(property_t);
NSLog(@"%s", name);

打印:

TestProject[20119:217782] age

代码 shadowheart

class_copyPropertyList

/** 
 * Describes the properties declared by a class.
 * 
 * @param cls The class you want to inspect.
 * @param outCount On return, contains the length of the returned array. 
 *  If \e outCount is \c NULL, the length is not returned.        
 * 
 * @return An array of pointers of type \c objc_property_t describing the properties 
 *  declared by the class. Any properties declared by superclasses are not included. 
 *  The array contains \c *outCount pointers followed by a \c NULL terminator. You must free the array with \c free().
 * 
 *  If \e cls declares no properties, or \e cls is \c Nil, returns \c NULL and \c *outCount is \c 0.
 */
 // 得到类属性列表
OBJC_EXPORT objc_property_t _Nonnull * _Nullable
class_copyPropertyList(Class _Nullable cls, unsigned int * _Nullable outCount)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

unsigned int propertyCount;
objc_property_t *propertyList = class_copyPropertyList([Person class], &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++) {
    objc_property_t property_t = propertyList[I];
    NSLog(@"%s", property_getName(property_t));
}
free(propertyList);

打印:

TestProject[20198:223479] manTest
TestProject[20198:223479] age

月晷

class_getIvarLayout

/** 
 * Returns a description of the \c Ivar layout for a given class.
 * 
 * @param cls The class to inspect.
 * 
 * @return A description of the \c Ivar layout for \e cls.
 */
 // 返回一个类的修饰属性
OBJC_EXPORT const uint8_t * _Nullable
class_getIvarLayout(Class _Nullable cls)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

const uint8_t *array_s = class_getIvarLayout([Person class]);

打印:

(lldb) po array_s
"\x01"

代码 moondial

class_getWeakIvarLayout

/** 
 * Returns a description of the layout of weak Ivars for a given class.
 * 
 * @param cls The class to inspect.
 * 
 * @return A description of the layout of the weak \c Ivars for \e cls.
 */
 //  待续
OBJC_EXPORT const uint8_t * _Nullable
class_getWeakIvarLayout(Class _Nullable cls)
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

打印:

蘑菇农场

property_getName

/* Working with Properties */

/** 
 * Returns the name of a property.
 * 
 * @param property The property you want to inquire about.
 * 
 * @return A C string containing the property's name.
 */
 // 返回属性的名字
OBJC_EXPORT const char * _Nonnull
property_getName(objc_property_t _Nonnull property) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

objc_property_t property_t = class_getProperty([Person class], "age");
const char *name = property_getName(property_t);
NSLog(@"%s", name);

打印:

TestProject[21844:408300] age

代码 mushroom_farm

property_getAttributes

/** 
 * Returns the attribute string of a property.
 * 
 * @param property A property.
 *
 * @return A C string containing the property's attributes.
 * 
 * @note The format of the attribute string is described in Declared Properties in Objective-C Runtime Programming Guide.
 */
 // 返回属性的属性字符串
OBJC_EXPORT const char * _Nullable
property_getAttributes(objc_property_t _Nonnull property) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

id lenderClass = objc_getClass("Person");
unsigned int outCount, I;
objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
    objc_property_t property = properties[I];
    fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
}

打印:

manTest T@"NSString",&,N,V_manTest
age Tq,N,V_age

打印解释:

bf88必发唯一官网 5

image

迷你国际象棋

objc_property_attribute_t

结构:

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

独具匠心骑士象棋白色/黄色

property_copyAttributeList

/** 
 * Returns an array of property attributes for a property. 
 * 
 * @param property The property whose attributes you want copied.
 * @param outCount The number of attributes returned in the array.
 * 
 * @return An array of property attributes; must be free'd() by the caller. 
 */
 // 返回属性的属性列表值
OBJC_EXPORT objc_property_attribute_t * _Nullable
property_copyAttributeList(objc_property_t _Nonnull property,
                           unsigned int * _Nullable outCount)
    OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);

代码:

id lenderClass = objc_getClass("Person");
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
    objc_property_t property = properties[I];
    fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));

    unsigned int attrCount = 0;
    objc_property_attribute_t *attrs = property_copyAttributeList(property, &attrCount);
    for (unsigned int j = 0; j < attrCount; j++) {
        objc_property_attribute_t attr = attrs[j];
        const char *name = attr.name;
        const char *value = attr.value;
        NSLog(@"属性的描述: %s 值: %s", name, value);
    }
}

打印:

manTest T@"NSString",&,N,V_manTest
TestProject[23001:481165] 属性的描述: T 值: @"NSString"
TestProject[23001:481165] 属性的描述: & 值: 
TestProject[23001:481165] 属性的描述: N 值: 
TestProject[23001:481165] 属性的描述: V 值: _manTest

age Tq,N,V_age
TestProject[23001:481165] 属性的描述: T 值: q
TestProject[23001:481165] 属性的描述: N 值: 
TestProject[23001:481165] 属性的描述: V 值: _age

代码 trinket_30/31

/** 
 * Returns the value of a property attribute given the attribute name.
 * 
 * @param property The property whose attribute value you are interested in.
 * @param attributeName C string representing the attribute name.
 *
 * @return The value string of the attribute \e attributeName if it exists in
 *  \e property, \c nil otherwise. 
 */
 // 通过属性的名字得到属性的值
OBJC_EXPORT char * _Nullable
property_copyAttributeValue(objc_property_t _Nonnull property,
                            const char * _Nonnull attributeName)
    OBJC_AVAILABLE(10.7, 4.3, 9.0, 1.0, 2.0);

代码:

id lenderClass = objc_getClass("Person");
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList(lenderClass, &outCount);
for (unsigned int i = 0; i < outCount; i++) {
    objc_property_t property = properties[I];
    fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));

    unsigned int attrCount = 0;
    objc_property_attribute_t *attrs = property_copyAttributeList(property, &attrCount);
    for (unsigned int j = 0; j < attrCount; j++) {
        objc_property_attribute_t attr = attrs[j];
        const char *name = attr.name;
        const char *value = attr.value;
        const char *test = property_copyAttributeValue(property, name);
        NSLog(@"属性的描述: %s 值: %s", name, value);
        NSLog(@"test -- %s", test);
    }
}

打印:

manTest T@"NSString",&,N,V_manTest
TestProject[23099:496137] 属性的描述: T 值: @"NSString"
TestProject[23099:496137] test -- @"NSString"
TestProject[23099:496137] 属性的描述: & 值: 
TestProject[23099:496137] test -- 
TestProject[23099:496137] 属性的描述: N 值: 
TestProject[23099:496137] test -- 
TestProject[23099:496137] 属性的描述: V 值: _manTest
TestProject[23099:496137] test -- _manTest

age Tq,N,V_age
TestProject[23099:496137] 属性的描述: T 值: q
TestProject[23099:496137] test -- q
TestProject[23099:496137] 属性的描述: N 值: 
TestProject[23099:496137] test -- 
TestProject[23099:496137] 属性的描述: V 值: _age
TestProject[23099:496137] test -- _age

迷你战车象棋白色/青色

动态库

代码 trinket_28/29

objc_copyImageNames

/* Working with Libraries */

/** 
 * Returns the names of all the loaded Objective-C frameworks and dynamic
 * libraries.
 * 
 * @param outCount The number of names returned.
 * 
 * @return An array of C strings of names. Must be free()'d by caller.
 */
 // 返回所有已加载的 Objective-C 框架和动态库的名称
OBJC_EXPORT const char * _Nonnull * _Nonnull
objc_copyImageNames(unsigned int * _Nullable outCount) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

unsigned int outCount;
const char **nameList = objc_copyImageNames(&outCount);
for (unsigned int i = 0; i < outCount; i++) {
    const char *name = nameList[I];
    NSLog(@"name : %s", name);
}
free(nameList);

打印:

TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/introspection/libdispatch.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libxpc.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_trace.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libsystem_network.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libobjc.A.dylib
TestProject[34100:1278609] name : /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/usr/lib/libate.dylib

迷你主教象棋白色/灰色

class_getImageName

/** 
 * Returns the dynamic library name a class originated from.
 * 
 * @param cls The class you are inquiring about.
 * 
 * @return The name of the library containing this class.
 */
 // 返回这个类 源动态库名称
OBJC_EXPORT const char * _Nullable
class_getImageName(Class _Nullable cls) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码:

const char *libraryName = class_getImageName([UIViewController class]);
NSLog(@"name: %s", libraryName);

打印:

TestProject[34267:1284976] name: /Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/UIKit

代码 trinket_15/16

objc_copyClassNamesForImage

代码:

unsigned int outCount;
const char **nameList = objc_copyClassNamesForImage("/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 10.2.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/UIKit.framework/UIKit", &outCount);
for (unsigned i = 0; i < outCount; i++) {
    const char *name = nameList[I];
    NSLog(@"name: %s", name);
}

打印:

部分数据:
TestProject[34509:1304491] name: _UIPreviewPresentationPlatterView
TestProject[34509:1304491] name: UIKeyboardUISettings
TestProject[34509:1304491] name: _UIPickerViewTopFrame
TestProject[34509:1304491] name: _UIOnePartImageView
TestProject[34509:1304491] name: _UIPickerViewSelectionBar
TestProject[34509:1304491] name: _UIPickerWheelView
TestProject[34509:1304491] name: _UIPickerViewTestParameters
TestProject[34509:1304491] name: UIPickerView

陶轮

objc_setAssociatedObject

/** 
 * 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.”
 * 
 * @see objc_setAssociatedObject
 * @see objc_removeAssociatedObjects
 */
 // 给已经存在的类添加私有属性,并且设置关联级别
OBJC_EXPORT void
objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
                         id _Nullable value, objc_AssociationPolicy policy)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

代码:

UIImage+AddProperty.h
#import <UIKit/UIKit.h>
@interface UIImage (AddProperty)
@property(nonatomic,strong)NSString *imageWidth;
@end

UIImage+AddProperty.m
#import "UIImage+AddProperty.h"
#import <objc/runtime.h>

@implementation UIImage (AddProperty)

-(NSString *)imageWidth{
    return objc_getAssociatedObject(self, _cmd);
}

-(void)setImageWidth:(NSString *)imageWidth{
    objc_setAssociatedObject(self, @selector(imageWidth), imageWidth, OBJC_ASSOCIATION_ASSIGN);
}

@end

代码 sculptingtable

objc_getAssociatedObject

/** 
 * 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.
 * 
 * @see objc_setAssociatedObject
 */
 // 通过对象和 key 得到一个关联值
OBJC_EXPORT id _Nullable
objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

国际象棋

objc_removeAssociatedObjects

/** 
 * 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.
 * 
 * @see objc_setAssociatedObject
 * @see objc_getAssociatedObject
 */
 // 删除给定对象的所有的关联
OBJC_EXPORT void
objc_removeAssociatedObjects(id _Nonnull object)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

(marble为白色 stone为粉青色,输入代码时请自行按照气象改变颜色)

Class

烟斗

objc_allocateClassPair

/* Adding Classes */

/** 
 * Creates a new class and metaclass.
 * 
 * @param superclass The class to use as the new class's superclass, or \c Nil to create a new root class.
 * @param name The string to use as the new class's name. The string will be copied.
 * @param extraBytes The number of bytes to allocate for indexed ivars at the end of 
 *  the class and metaclass objects. This should usually be \c 0.
 * 
 * @return The new class, or Nil if the class could not be created (for example, the desired name is already in use).
 * 
 * @note You can get a pointer to the new metaclass by calling \c object_getClass(newClass).
 * @note To create a new class, start by calling \c objc_allocateClassPair. 
 *  Then set the class's attributes with functions like \c class_addMethod and \c class_addIvar.
 *  When you are done building the class, call \c objc_registerClassPair. The new class is now ready for use.
 * @note Instance methods and instance variables should be added to the class itself. 
 *  Class methods should be added to the metaclass.
 */
功能:创建一个新的类和这个元类
去创建一个新的类开始通过objc_allocateClassPair,然后设置类的属性和功能用class_addMethod和class_addIvar,实例化方法和实例化变量应该被添加到类本身,类应该添加到元类中。
OBJC_EXPORT Class _Nullable
objc_allocateClassPair(Class _Nullable superclass, const char * _Nonnull name, 
                       size_t extraBytes) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

代码 chesspiece_pipe_marble/stone

objc_registerClassPair

/** 
 * Registers a class that was allocated using \c objc_allocateClassPair.
 * 
 * @param cls The class you want to register.
 */
// 注册创建的类
OBJC_EXPORT void
objc_registerClassPair(Class _Nonnull cls) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

禁卫

代码 chesspiece_pawn_marble/stone

牛角帽

代码 chesspiece_hornucopia_marble/stone

皇后

代码 chesspiece_muse_marble/stone

国王

代码 chesspiece_formal_marble/stone

骑士

代码 chesspiece_knight_marble/stone

主教

代码 chesspiece_bishop_marble/stone

战车

代码 chesspiece_rook_marble/stone

茶几

代码 endtable

巨型蜂巢

代码 beequeenhivegrown

蜜蜂皇后

代码 beequeen

雄峰/鬼蜂

代码 beegurd

蜂王浆

代码 royal_jelly

霓虹软糖

代码 jellybean

丽江豌豆

代码 marblebean

抚顺石灌木

代码 marbleshrub_tall

木栅栏

代码 fence

可放置的木栅栏

代码 fence_item

木门

代码 fence_gate

可放置的木门

代码 fence_gate_item

岩石小穴

代码 critterlab

冷冻虫卵

代码 lavae_cocoon

小浣猫

代码 critterlab_kitten

小鬣犬

代码 critterlab_puppy

小钢羊

代码 critterlab_lamb

小龙蝇

代码 critterlab_dragonling

小格罗姆

代码 critterlab_glomling

无睛鹿

代码 deer

小木牌

代码 minisign_item

蜂蜡

代码 beeswax

蜡纸

代码 waxpaper

捆绑打包

代码 bundlewrap

包裹

代码 bundle

蜂冠

代码 hivehat

玩家聚焦:

饔飧不继手机版怎么用代码 手机版代码

饔飧不继联机版代码大全 联机版控制台秘籍

饔飧不继海难代码大全 海难代码怎么用

并日而食手机版怎么作弊 手机版作弊教程

饔飧不给沙滩BOSS代码有哪些 沙滩BOSS代码都有什么

啼饥号寒秘籍代码大全 手机版秘籍

并日而食手机版攻略 饥馑手机版新手攻略教程

4399饔飧不给攻略专区

bf88必发唯一官网 6

bf88必发唯一官网 7

bf88必发唯一官网 8

bf88必发唯一官网 9

bf88必发唯一官网 10

bf88必发唯一官网 11

打闹周边
bf88必发唯一官网 12

建筑大全
bf88必发唯一官网 13

娱乐问答
bf88必发唯一官网 14

MOD大全bf88必发唯一官网 15

物品齐全bf88必发唯一官网 16

关怀饥馑攻略专区,左右饥馑最新信息,还有强大攻略库、实用工具!**

bf88必发唯一官网 17

越来越多杰出攻略,请关心4399饔飧不继攻略专区,或者在百度上搜索“4399饥荒,找到大家的专区,即可在第一时间得到饥馑的新型相关内容。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图