一些公用类:

@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. Java基础--常用API--日期相关API

    一.java.util.Date 1.只用于显示系统时间,其大部分操作由Calendar代替. 格林威治时间(GMT):指的是1970年1月1日0时,不同地区有时间差. 默认输出格式:星期.月.日.时 ...

  2. 洛咕11月月赛部分题解 By cellur925

    听说是你谷史上最水月赛?我不听我最菜 T1:终于结束的起点 月天歌名好评 给你一个模数 \(M\),请你求出最小的 \(n > 0\),使得\(fib(n)\) \(mod\) \(m=0\), ...

  3. 页面出现滚动条时,body里面的内容不能自动居中?

    弹窗后允许页面滚动 这种方式通常使用 position: absolute; ,可以看看我做的这个 Demo.主要用来应对弹窗内容很大很多的情况,超过了屏幕的宽高需要产生滚动条来方便浏览者查看.有一些 ...

  4. Mysql数据表的增删改查

    ---恢复内容开始--- Mysql数据表的增删改查 1.创建表   语法:CREATE TABLE 表名(字段1,字段2,字段3.......) CREATE TABLE `users` ( `us ...

  5. GIT使用笔记一:GIT初始化配置

    本人系统环境:centos6.5 下 LNMP centos下git安装很简单sudo yum install gitOK 可先进行git 的全局配置 用户信息 git config --global ...

  6. CountDownLatch MyUncaughtExceptionHandler

    package com.yd.wmsc.util; import java.text.SimpleDateFormat; import java.util.Date; import java.util ...

  7. ]NET Core Lucene.net和PanGu分词实现全文检索

    Lucene.net和PanGu分词实现全文检索 Lucene.net(4.8.0) 学习问题记录五: JIEba分词和Lucene的结合,以及对分词器的思考   前言:目前自己在做使用Lucene. ...

  8. 爬虫(BeautifulSoup4)——安装

    环境:python3 win10 安装这个心好累啊!网上找了很多办法都安装不成功,后来换了几个安装包,最后4.4.1版本的包终于能用了! https://blog.csdn.net/www520507 ...

  9. 《javascript设计模式》笔记之第十章 和 第十一章:门面模式和适配器模式

    第十章:门面模式 一:门面模式的作用 简化已有的api,使其更加容易使用 解决浏览器的兼容问题 二:门面模式的本质 门面模式的本质就是包装已有的api来简化操作   三:门面模式的两个简单例子 下面这 ...

  10. Kendo MVVM 数据绑定(四) Disabled/Enabled

    Kendo MVVM 数据绑定(四) Disabled/Enabled Disabled 和 Enabled 绑定可以根据 ViewModel 的某个属性值的 true,false 来设置 DOM 元 ...