一些公用类:

@interface ClassCustomClass :NSObject{

NSString *varTest1;

NSString *varTest2;

NSString *varTest3;

}

@property (nonatomic,assign)NSString *varTest1;

@property (nonatomic,assign)NSString *varTest2;

@property (nonatomic,assign)NSString *varTest3;

- (void) fun1;

@end

@implementation ClassCustomClass

@synthesize varTest1, varTest2, varTest3;

- (void) fun1 {

NSLog(@"fun1");

}

@end

@interface ClassCustomClassOther :NSObject {

int varTest2;

}

- (void) fun2;

@end

@implementation ClassCustomClassOther

- (void) fun2 {

NSLog(@"fun2");

}

@end

@interface ClassPropertyViewCtr () {

float myFloat;

ClassCustomClass *allobj;

}

myFloat = 2.34f;

6、获取一个类的所有方法:

- (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);

}

}

打印结果(部分):

2013-07-26 16:07:03.972 HighOC[7021:c07] _screen

2013-07-26 16:07:03.973 HighOC[7021:c07] applicationWillSuspend

2013-07-26 16:07:03.973 HighOC[7021:c07] _tryBecomeRootViewControllerInWindow:

2013-07-26 16:07:03.973 HighOC[7021:c07] isViewLoaded

2013-07-26 16:07:03.974 HighOC[7021:c07] view

......................

7、获取一个类的所有属性:

- (void) propertyNameList

{

u_int count;

objc_property_t *properties=class_copyPropertyList([UIViewControllerclass], &count);

for (int i =
0; i < count ; i++)

{

const char* propertyName =property_getName(properties[i]);

NSString *strName = [NSString stringWithCString:propertyNameencoding:NSUTF8StringEncoding];

NSLog(@"%@",strName);

}

}

打印结果(部分)

2013-07-26 16:09:42.182 HighOC[7041:c07] tabBarItem

2013-07-26 16:09:42.184 HighOC[7041:c07] tabBarController

2013-07-26 16:09:42.185 HighOC[7041:c07] splitViewController

2013-07-26 16:09:42.186 HighOC[7041:c07] navigationItem

2013-07-26 16:09:42.186 HighOC[7041:c07] hidesBottomBarWhenPushed

...............

8、获取/设置类的属性变量

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

- (void) getInstanceVar {

float myFloatValue;

object_getInstanceVariable(self,"myFloat", (void*)&myFloatValue);

NSLog(@"%f", myFloatValue);

}

//设置全局变量的值

- (void) setInstanceVar {

float newValue = 10.00f;

unsigned int addr = (unsignedint)&newValue;

object_setInstanceVariable(self,"myFloat", *(float**)addr);

NSLog(@"%f",
myFloat);

}

9、判断类的某个属性的类型

- (void) getVarType {

ClassCustomClass *obj = [ClassCustomClassnew];

Ivar var = class_getInstanceVariable(object_getClass(obj),"varTest1");

const char* typeEncoding =ivar_getTypeEncoding(var);

NSString *stringType =  [NSStringstringWithCString:typeEncodingencoding:NSUTF8StringEncoding];

if ([stringType hasPrefix:@"@"]) {

// handle class case

NSLog(@"handle class case");

} else if ([stringTypehasPrefix:@"i"]) {

// handle int case

NSLog(@"handle int case");

} else if ([stringTypehasPrefix:@"f"]) {

// handle float case

NSLog(@"handle float case");

} else

{

}

}

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

- (NSString *)nameOfInstance:(id)instance

{

unsigned int numIvars =0;

NSString *key=nil;

//Describes the instance variables declared by a class.

Ivar * ivars = class_copyIvarList([ClassCustomClassclass], &numIvars);

for(int i =
0; i < numIvars; i++) {

Ivar thisIvar = ivars[i];

const char *type =ivar_getTypeEncoding(thisIvar);

NSString *stringType =  [NSStringstringWithCString:typeencoding:NSUTF8StringEncoding];

//不是class就跳过

if (![stringType hasPrefix:@"@"]) {

continue;

}

//Reads the value of an instance variable in an object. object_getIvar这个方法中,当遇到非objective-c对象时,并直接crash

if ((object_getIvar(allobj, thisIvar) == instance)) {

// Returns the name of an instance variable.

key = [NSStringstringWithUTF8String:ivar_getName(thisIvar)];

break;

}

}

free(ivars);

return key;

}

测试代码:

allobj = [ClassCustomClassnew];

allobj.varTest1 =@"varTest1String";

allobj.varTest2 =@"varTest2String";

allobj.varTest3 =@"varTest3String";

NSString *str = [selfnameOfInstance:@"varTest1String"];

NSLog(@"str:%@", str);

打印结果:

2013-07-26 16:26:26.271 HighOC[7081:c07] str:varTest1

IOS高级开发~Runtime(一)

http://blog.csdn.net/zfpp25_/article/details/9496705

IOS高级开发~Runtime(二)

http://blog.csdn.net/zfpp25_/article/details/9497187

IOS高级开发~Runtime(三)

http://blog.csdn.net/zfpp25_/article/details/9497721

IOS高级开发~Runtime(四)

http://blog.csdn.net/zfpp25_/article/details/9498233

(转发)IOS高级开发~Runtime(二)的更多相关文章

  1. (转发)IOS高级开发~Runtime(四)

    用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...

  2. (转发)IOS高级开发~Runtime(三)

    11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...

  3. (转发)IOS高级开发~Runtime(一)

    IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...

  4. IOS 高级开发 runtime(二)

    二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...

  5. IOS高级开发 runtime(一)

    一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...

  6. iOS 高级开发 runtime(三)

    三 .动态添加方法 我们可以通过runtime动态地添加方法.那么到底啥叫动态添加方法呢?动态添加方法就是当我们程序运行时才知道我们应该调用哪个方法.我们首先需要了解这一点,当我们编写完一段代码后,我 ...

  7. iOS蓝牙开发(二)蓝牙相关基础知识

    原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...

  8. IOS高级开发之多线程(四)NSOperation

    1.什么是NSOperation,NSOperationQueue? NSOperation是一个抽象的基类,表示一个独立的计算单元,可以为子类提供有用且线程安全的建立状态,优先级,依赖和取消等操作. ...

  9. IOS高级开发~Runtime(二)

    #import <Foundation/Foundation.h> @interface CustomClass : NSObject { NSString *varTest1; NSSt ...

随机推荐

  1. 背包dp

    一大波模板正在靠近 1.01背包 问题:有n件物品和一个容量为v的背包,第i件物品的费用(即体积)是w[i],价值是v[i],求解将哪些物品装入背包可使这些物品的费用和不超过背包容量,且价值总和最大. ...

  2. sublime text 3 添加 javascript 代码片段 ( snippet )

    例如:新建console.log();的快捷键为 co 环境:windows 7 step1: Tools -> New Snippet <snippet> <content& ...

  3. CF438D The Child and Sequence 线段树

    给定数列,区间查询和,区间取模,单点修改. n,m小于10^5 ...当区间最值小于模数时,就直接返回就好啦~ #include<cstdio> #include<iostream& ...

  4. Codeforces 185D(发现性质、欧拉定理)

    学到的东西 不知道gcd时不妨先假设为d,然后为了满足全部式子说不定可以得到d的取值范围. 幂上带幂考虑欧拉定理的使用. 有几个特殊情况会破坏公式的完美不要紧,看看特殊情况是否能简便地判定. 连乘公式 ...

  5. ADC5513

    一 C5513 u32 ADC5513_GetValue(void){  u32 ADValue,i;  bool data_bit = false;   C5513_SCK=0;  C5513_CS ...

  6. SQL server 游标,随机数使用

    SELECT * FROM [goods] DECLARE test_cursor CURSOR scroll FOR SELECT goods_no, [unitprice] FROM [goods ...

  7. h5点击区域和实际区域对不上

    点击区域和实际区域对不上 然后点击后触发的其实是上面的区域,会导致事件触发错误

  8. 微信支付(java版本)_支付结果通知

    应用场景: 支付完成后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答. 对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新 ...

  9. oo第三单元总结

    JML梳理 1. JM语法一般结构 public instance //jml中操作数据,并不要求实现 public invariant //不变式 public constraint //约束 no ...

  10. android pm命令

    把网络apk下载到盒子或者其他安卓设备上 1.adb push windows的原路径 android设备的路径 2.pm install android设备的路径 注意:这里pm命令是安卓设备才有的 ...