美文网首页
实例变量、property、method

实例变量、property、method

作者: ganser | 来源:发表于2016-07-28 11:27 被阅读11次

    实例变量是iOS类内的变量,而property是属性,为方便增加set和get的方法。

    #import "ViewController.h"
    #import <objc/runtime.h>
    
    @interface ViewController () {
        NSInteger aaa;
        NSString *bbb;
    }
    @property (nonatomic, copy) NSString *agan;
    @end
    
    @implementation ViewController
    @synthesize agan = _HAHA;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        //打印所有的properties
        u_int count;
        objc_property_t *properties= class_copyPropertyList([self class], &count);
        for (int i = 0; i < count ; i++){
            const char* propertyName = property_getName(properties[i]);
            NSString *strName = [NSString  stringWithCString:propertyName encoding:NSUTF8StringEncoding];
            NSLog(@"%@",strName);
        }
        
        NSLog(@"----------------------分割线------------------------");
        
        // 打印所有的成员变量
        Ivar *ivars = class_copyIvarList([self class], &count);
        for (int i = 0; i<count; i++) {
            // 取出成员变量
            Ivar ivar = *(ivars + i);
            // 打印成员变量名字
            NSLog(@"%s", ivar_getName(ivar));
            // 打印成员变量的数据类型
            NSLog(@"%s", ivar_getTypeEncoding(ivar));
        }
        
        NSLog(@"----------------------分割线------------------------");
        
        // 打印所有的方法
        unsigned int methCount = 0;
        Method *meths = class_copyMethodList([self class], &methCount);
        for(int i = 0; i < methCount; i++) {
            Method meth = meths[i];
            SEL sel = method_getName(meth);
            const char *name = sel_getName(sel);
            NSLog(@"%s", name);
        }
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:实例变量、property、method

          本文链接:https://www.haomeiwen.com/subject/fedajttx.html