一些公用类:

@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. 剑指Offer的学习笔记(C#篇)-- 反转链表

    题目描述 输入一个链表,反转链表后,输出新链表的表头. 一 . 概念普及 关于线性表等相关概念请点击这里. 二 . 实现方法 目前,可以有两种方法实现该要求. 方法一:借助外部空间实现.这里可以将单链 ...

  2. sublime text 3 的emmet 添加自定义 html 片段

    比如想在html写 jquery 直接添加jquery地址,打开 ‘首选项->Package Setting->Emmet->Settings - User’, css也可以如法炮制 ...

  3. JSON 返回值JSONPath Syntax

    {"sys":"ROC","code":0,"messages":"获取列表成功!","d ...

  4. PAT刷题记录

    1. 1007 素数对猜想 :运行超时 算法问题还是语言本身效率低下? import math def is_prime(num): if num == 2: return num temp = in ...

  5. BZOJ 4551: [Tjoi2016&Heoi2016]树 并查集(&&图论?)

    反向操作,先把所有的标记都打上(记得统计标记的数目),然后依次撤销,合并到自己的上一个点pre,即fa[u]=getf(pre[u]) #include<cstdio> #include& ...

  6. 这个匿名对象没有实现IComparable接口

    https://www.cnblogs.com/felixnet/p/5193086.html https://docs.microsoft.com/zh-cn/dotnet/api/system.i ...

  7. Sticky Footer的实现

    Sticky Footer即绝对底部,是一种常用的布局方式,页脚在内容区未超出窗口高度时一直保持在最底部显示,而超过窗口高度时则保持在内容区最底部. 有两种实现方法: 第一种:经典的实现方式 html ...

  8. STM32之ADC(内部基准电压,参考电压)

    转 STM32内部参照电压VREFIN的使用 https://blog.csdn.net/uncle_guo/article/details/50625660 每个STM32芯片都有一个内部的参照电压 ...

  9. memcache和iptables开启11211端口

    linux下安装完memcached后,netstat -ant | grep LISTEN 看到memcache用的11211端口已在监听状态,但建立php文件连接测试发现没有输出结果,iptabl ...

  10. SpringMVC注解方式与文件上传

    目录: springmvc的注解方式 文件上传(上传图片,并显示) 一.注解 在类前面加上@Controller 表示该类是一个控制器在方法handleRequest 前面加上 @RequestMap ...