zoukankan      html  css  js  c++  java
  • 通过runtime验证property声明是否会创建ivar

    本文做关于ivar的测试:

    1.@property (nonatomic, strong)NSStirng*property1;这个声明是否编译器会生成一个ivar变量

    2.property声明中声明为weak

    3.property声明为assin

    具体测试代码如下:

    #import <Foundation/Foundation.h>
    
    @interface IvarTest : NSObject
    {
        NSString*ivar1;
        NSString*ivar2;
    }
    @property(nonatomic, strong)NSString*property1;
    @property(nonatomic, strong)NSString*property2;
    @end
    
    
    
    #import "IvarTest.h"
    @interface IvarTest()
    
    @property(nonatomic, strong)NSString*property3;
    @property(nonatomic, strong)NSString*property4;
    @property(nonatomic, weak)NSString*property5;
    @property(nonatomic, assign)NSString*property6;
    
    @end
    
    @implementation IvarTest
    {
        NSString*ivar3;
        NSString*ivar4;
    }
    @end
    
    
    #import "ViewController.h"
    #import "IvarTest.h"
    #import <objc/runtime.h>
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        unsigned int count;
        Ivar *ivars = class_copyIvarList([IvarTest class], &count);
        
        for (unsigned int i = 0; i < count; ++i)
        {
            Ivar ivar = ivars[i];
            const char *ivarName = ivar_getName(ivar);
            NSString*ivarNameString = [NSString stringWithUTF8String:ivarName];
            NSLog(@"%@",ivarNameString);
        }
    
    }
    
    @end
    

      运行后打印结果为:

    2016-04-28 16:40:13.979 IvarTest[31253:260489] ivar1

    2016-04-28 16:40:13.979 IvarTest[31253:260489] ivar2

    2016-04-28 16:40:13.979 IvarTest[31253:260489] ivar3

    2016-04-28 16:40:13.979 IvarTest[31253:260489] ivar4

    2016-04-28 16:40:13.980 IvarTest[31253:260489] _property1

    2016-04-28 16:40:13.980 IvarTest[31253:260489] _property2

    2016-04-28 16:40:13.980 IvarTest[31253:260489] _property3

    2016-04-28 16:40:13.980 IvarTest[31253:260489] _property4

    2016-04-28 16:40:13.980 IvarTest[31253:260489] _property5

    2016-04-28 16:40:13.980 IvarTest[31253:260489] _property6

     

    说明,property声明后,就会生成对应的ivar变量,property声明的是weak,assign都会生成对应的ivar变量

  • 相关阅读:
    APP测试--功能测试
    笨方法学python--读文件
    笨方法学python--参数,解包,变量
    Jmeter工具学习(四)——插件安装及使用(转载)
    JMeter工具学习(三)——获取全局变量 token
    FROM_UNIXTIME()时间戳转换函数
    软件测试用例编写规范总结(转载)
    Jmeter之Bean shell使用(二)(转载)
    JMeter工具学习(一)工具使用详细介绍
    JMeter工具学习(二)——获取登录 token
  • 原文地址:https://www.cnblogs.com/wxm5558/p/5443419.html
Copyright © 2011-2022 走看看