Runtime学习2



#warning 获取一个类的实例所有方法


#warning 获取一个类的所有属性


#warning 获取/设置类的属性变量\

获取全局变量的值   myFloat为类的一个属性变量)


#warning 判断类的某个属性的类型



#warning 通过属性的值来获取其属性的名字测试


#warning 通过属性的值来获取其属性的名字(反射机制)





1.CustomClass

//
//  CustomClass.h
//  Runtime学习2
//
//  Created by 陆巧怡 on 15/8/3.
//  Copyright (c) 2015年 Simon. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CustomClass : NSObject
{
    NSString *text1;
    NSDictionary *text2;
    NSMutableArray *text3;

}

@property (nonatomic, strong) NSString *text1;
@property (nonatomic, strong) NSDictionary *text2;
@property (nonatomic, strong) NSMutableArray *text3;
-(void)fun1;
@end

//
//  CustomClass.m
//  Runtime学习2
//
//  Created by 陆巧怡 on 15/8/3.
//  Copyright (c) 2015年 Simon. All rights reserved.
//

#import "CustomClass.h"

@implementation CustomClass
@synthesize text1,text2,text3;

-(void)fun1{
    
    NSLog(@"fun1");
}

@end


2.CustomClassOther

//
//  CustomClassOther.m
//  Runtime学习2
//
//  Created by 陆巧怡 on 15/8/3.
//  Copyright (c) 2015年 Simon. All rights reserved.
//

#import "CustomClassOther.h"

@implementation CustomClassOther

-(void)fun2{
    
    NSLog(@"fun2");
}
@end

3.CustomMethod

//
//  CustomMethod.h
//  Runtime学习2
//
//  Created by 陆巧怡 on 15/8/4.
//  Copyright (c) 2015年 Simon. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CustomMethod : NSObject
-(void)justLog1;
-(void)justLog2;
@end

//
//  CustomMethod.m
//  Runtime学习2
//
//  Created by 陆巧怡 on 15/8/4.
//  Copyright (c) 2015年 Simon. All rights reserved.
//

#import "CustomMethod.h"

@implementation CustomMethod
-(void)justLog1{
    
    NSLog(@"justLog1");
}

-(void)justLog2{
    
    NSLog(@"justLog2");
    
}
@end

4.测试

//
//  ViewController.m
//  Runtime学习2
//
//  Created by 陆巧怡 on 15/8/3.
//  Copyright (c) 2015年 Simon. All rights reserved.
//

#import "ViewController.h"
#import "CustomClass.h"
#import "CustomClassOther.h"
#import "CustomMethod.h"
#include <objc/runtime.h>
@interface ViewController ()
{
    float myFloat;
    CustomClass *objectClass;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    myFloat = 2.34f;
    
    
    
}

#warning 获取一个类的实例所有方法
-(void)getClassAllMethod{
    
    u_int count;
    Method *methods = class_copyMethodList([UIViewController class], &count);
    for (int i = 0; i<count; i++) {
        SEL name = method_getName(methods[i]);
        NSString *strName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];
        NSLog(@"%@",strName);
    }
}

#warning 获取一个类的所有属性
-(void)getClassAllPropetyNameList{
    u_int count;
    objc_property_t * propertys = class_copyPropertyList([CustomClass class], &count);
    for (int i = 0; i<count; i++) {
        const char *charname = property_getName(propertys[i]);
        NSLog(@"%@",[NSString stringWithCString:charname encoding:NSUTF8StringEncoding]);
    }
}

#warning 获取/设置类的属性变量\
获取全局变量的值   (myFloat 为类的一个属性变量)
- (void)getInstanceVar{
    float myFloatValue;
    object_getInstanceVariable(self, "myFloat", (void*)&myFloatValue);
    NSLog(@"%f",myFloatValue);
}

- (void)setInstanceVar{
    float newValue = 0.00f;
    unsigned int addr = (unsigned int)&newValue;
    object_setInstanceVariable(self,"newValue", (void *)(float**)addr);
    NSLog(@"%f", myFloat);
    //这个设置类的属性变量方法还不知道有什么用
}

#warning 判断类的某个属性的类型
-(void)getVarType{
    CustomClass *obj = [[CustomClass alloc]init];
    u_int count;
    //获取类的所有属性名字
    objc_property_t *propertys = class_copyPropertyList(object_getClass(obj), &count);
    
    for (int i =0; i<count; i++) {
        //获取属性名字
        const char *charname = property_getName(propertys[i]);
        //Ivar:定义对象的实例变量,包括类型和名字。
        Ivar var = class_getInstanceVariable(object_getClass(obj), charname);
        //获取成员变量的数据类型
        const char *typeEncoding = ivar_getTypeEncoding(var);
        NSString *stringType =  [NSString stringWithCString:typeEncoding encoding:NSUTF8StringEncoding];
        NSLog(@"%@",stringType);
    }
}

#warning 通过属性的值来获取其属性的名字测试
-(void)getNameOfInstance{
    
    objectClass = [[CustomClass alloc]init];
    objectClass.text1 = @"text1";
    NSString *str = [self nameOfInstance:@"text1"];
    NSLog(@"%@",str);
}

#warning 通过属性的值来获取其属性的名字(反射机制)
-(NSString *)nameOfInstance:(id)instance{
    
    unsigned int numIvars = 0;
    NSString *key = nil;
    Ivar *ivars= class_copyIvarList(object_getClass(objectClass), &numIvars);
    for (int i= 0; i<numIvars; i++) {
        //获取一个成员变量
        Ivar thisIvar = ivars[i];
        //获取改员变量的数据类型
        const char *typeEncoding = ivar_getTypeEncoding(thisIvar);
        NSString *stringType =[NSString stringWithCString:typeEncoding encoding:NSUTF8StringEncoding];
        //不是class就跳过
        if (![stringType hasPrefix:@"@"]) {
            continue;
        }
        if (object_getIvar(objectClass, thisIvar)==instance) {
            key = [NSString stringWithUTF8String:ivar_getName(thisIvar)];
            break;
        }
    }
    free(ivars);
    return key;
}

#warning 系统类的方法实现部分替换
-(void)methodExchange{
    
    Method m1 = class_getInstanceMethod([NSString class],@selector(lowercaseString));
    Method m2 = class_getInstanceMethod([NSString class],@selector(uppercaseString));
    method_exchangeImplementations(m1, m2);
    //系统小写方法已经切换成大写方法
    NSLog(@"%@", [@"sssAAAAss"lowercaseString]);
    //系统大写方法已经切换成小写方法
    NSLog(@"%@", [@"sssAAAAss"uppercaseString]);
    
}

#warning 自定义类的方法实现部分替换
- (void) methodSetImplementation {
    
    Method method = class_getInstanceMethod([CustomMethod class], @selector(justLog1));
    IMP originalIMP = method_getImplementation(method);
    Method m1 = class_getInstanceMethod([CustomMethod class], @selector(justLog2));
    //方法调换
    method_setImplementation(m1, originalIMP);
    
    CustomMethod *m2 = [[CustomMethod alloc]init];
    [m2 justLog2];
}



@end






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值