zoukankan      html  css  js  c++  java
  • IOS 运行时

    #import <Foundation/Foundation.h>
    #import "XMGPerson.h"
    #import "XMGStudent.h"
    #import <objc/runtime.h>
    #import <objc/message.h>
    
    /**
     1.什么是运行时(runtime)?
     1> 是苹果官方提供的一套纯C语言库
     2> 平时编写的OC代码最终都是转换成了运行时(runtime)C语言代码
     // 是苹果官方提供的非常实用的技术,在iOS开发中经常用到
     
     2.你在开发过程中, 怎么使用运行时?
     1> 可以获得一个类中声明的所有成员变量成员属性方法等等
     2> 可以动态添加成员变量成员属性方法等等
     3> 可以交换两个方法的实现(什么是method swizzle?)
     4> ...
     */
    
    /**
     * 输出c这个类的所有成员变量
     */
    void logAllIvars(Class c)
    {
        while (c) {
            // 获得所有的成员变量
            unsigned int outCount = 0;
            Ivar *ivarList = class_copyIvarList(c, &outCount);
            
            // 遍历所有的成员变量
            for (int i = 0; i < outCount; i++) {
                // 获得第i个成员变量
                Ivar ivar = ivarList[i];
                
                // 获得成员变量的名称和类型
                NSLog(@"%@ -> %s %s", c, ivar_getName(ivar), ivar_getTypeEncoding(ivar));
            }
            
            // 释放资源
            free(ivarList);
            
            // 获得父类
            c = class_getSuperclass(c);
        }
    }
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            logAllIvars([XMGStudent class]);
        }
        return 0;
    }
    
    void testCopyPropertyList()
    {
        unsigned int outCount = 0;
        objc_property_t *propertyList = class_copyPropertyList([XMGPerson class], &outCount);
        
        for (int i = 0; i < outCount; i++) {
            objc_property_t property = propertyList[i];
            NSLog(@"%s %s", property_getName(property), property_getAttributes(property));
        }
        
        free(propertyList);
    }
    
    void testCopyIvarList2()
    {
        /*
         内存管理:
         1.如果C语言函数名中包含了newcreatecopy
    etain等字眼, 那么这个函数创建出来的数据, 就需要手动释放
         2.常见的释放函数
         1> CFRelease(void *)
         2> ****Release(void *)
         3> free(void *)
         */
        
        // 获得所有的成员变量
        unsigned int outCount = 0;
        Ivar *ivarList = class_copyIvarList([XMGPerson class], &outCount);
        
        // 遍历所有的成员变量
        for (int i = 0; i < outCount; i++) {
            // 获得第i个成员变量
            //            Ivar ivar = *(ivarList + i);
            Ivar ivar = ivarList[i];
            
            // 获得成员变量的名称和类型
            NSLog(@"%s %s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
        }
        
        // 释放资源
        free(ivarList);
    }
    
    void testCopyIvarList()
    {
        // 成员变量 : Ivar (instance variable)
        
        // 成员变量的个数
        unsigned int outCount = 0;
        
        // 利用class_copyIvarList函数获得XMGPerson类的所有成员变量
        // Ivar *ivarList是一个指向Ivar(成员变量)的指针
        Ivar *ivarList = class_copyIvarList([XMGPerson class], &outCount);
        
        // 获得ivarList指向的成员变量
        Ivar firstIvar = ivarList[0];
    //    Ivar firstIvar = *(ivarList + 0);
    //    Ivar firstIvar = *ivarList;
        
        // 访问成员变量的内容
        NSLog(@"%s %s", ivar_getName(firstIvar), ivar_getTypeEncoding(firstIvar));
        
        // 释放资源
        free(ivarList);
        
        //        int age = 20;
        //
        //        int *p = &age;
        //
        //        int temp = *p;
        //
        //        *p = 100;
        //
        //        NSLog(@"%d %d", *p, age);
    }
    #import <Foundation/Foundation.h>
    
    @interface XMGPerson : NSObject
    {
        int _height;
        
        @private
        double _weight;
        
        @public
        double _money;
    }
    @property (nonatomic, assign) int age;
    @property (nonatomic, copy) NSString *name;
    @property (nonatomic,assign) BOOL rich;
    @property (nonatomic,assign) long test;
    @property (nonatomic, copy) void (^block)();
    @end

    #import "XMGPerson.h"
    
    @interface XMGPerson()
    {
        int _no2;
    }
    @end
    
    @implementation XMGPerson
    {
        CGFloat _score2;
    }
    
    @end

     #import "XMGPerson.h" @interface XMGStudent : XMGPerson @property (nonatomic, assign) double score; @end 

     #import "XMGStudent.h" @implementation XMGStudent @end 

    2015-10-13 17:00:09.332 学习运行时[37989:1071108] XMGStudent -> _score d

    2015-10-13 17:00:09.334 学习运行时[37989:1071108] XMGPerson -> _height i

    2015-10-13 17:00:09.334 学习运行时[37989:1071108] XMGPerson -> _weight d

    2015-10-13 17:00:09.334 学习运行时[37989:1071108] XMGPerson -> _money d

    2015-10-13 17:00:09.334 学习运行时[37989:1071108] XMGPerson -> _no2 i

    2015-10-13 17:00:09.335 学习运行时[37989:1071108] XMGPerson -> _score2 d

    2015-10-13 17:00:09.335 学习运行时[37989:1071108] XMGPerson -> _rich c

    2015-10-13 17:00:09.335 学习运行时[37989:1071108] XMGPerson -> _age i

    2015-10-13 17:00:09.335 学习运行时[37989:1071108] XMGPerson -> _name @"NSString"

    2015-10-13 17:00:09.335 学习运行时[37989:1071108] XMGPerson -> _test q

    2015-10-13 17:00:09.335 学习运行时[37989:1071108] XMGPerson -> _block @?

    2015-10-13 17:00:09.335 学习运行时[37989:1071108] NSObject -> isa #

  • 相关阅读:
    Pytorch 随机数种子设置
    python 利用 dictionary 的 .get() 操作,避免写 if-else
    PEP-8 or google 风格 python 代码风格和注释规范
    Vim 多文件切换使用
    Shell 变量及脚本使用
    python numpy 大矩阵运算容易内存爆炸
    Ubuntu 配置 Pytorch on Graph (PoG) 环境
    Markdown 学习笔记
    Linux-saltstack
    Python字符串详解
  • 原文地址:https://www.cnblogs.com/developer-ios/p/4875214.html
Copyright © 2011-2022 走看看