(转发)IOS高级开发~Runtime(二)
一些公用类:
@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
http://blog.csdn.net/zfpp25_/article/details/9496705
http://blog.csdn.net/zfpp25_/article/details/9497187
http://blog.csdn.net/zfpp25_/article/details/9497721
http://blog.csdn.net/zfpp25_/article/details/9498233
(转发)IOS高级开发~Runtime(二)的更多相关文章
- (转发)IOS高级开发~Runtime(四)
用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...
- (转发)IOS高级开发~Runtime(三)
11.系统类的方法实现部分替换 - (void) methodExchange { Method m1 = class_getInstanceMethod([NSStringclass],@selec ...
- (转发)IOS高级开发~Runtime(一)
IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...
- IOS 高级开发 runtime(二)
二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...
- IOS高级开发 runtime(一)
一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...
- iOS 高级开发 runtime(三)
三 .动态添加方法 我们可以通过runtime动态地添加方法.那么到底啥叫动态添加方法呢?动态添加方法就是当我们程序运行时才知道我们应该调用哪个方法.我们首先需要了解这一点,当我们编写完一段代码后,我 ...
- iOS蓝牙开发(二)蓝牙相关基础知识
原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...
- IOS高级开发之多线程(四)NSOperation
1.什么是NSOperation,NSOperationQueue? NSOperation是一个抽象的基类,表示一个独立的计算单元,可以为子类提供有用且线程安全的建立状态,优先级,依赖和取消等操作. ...
- IOS高级开发~Runtime(二)
#import <Foundation/Foundation.h> @interface CustomClass : NSObject { NSString *varTest1; NSSt ...
随机推荐
- uoj#38. 【清华集训2014】奇数国(线段树+数论)
传送门 不难看出就是要先求区间积,再求这个区间积的\(\varphi\) 因为\(\varphi(x)=x\times\frac{p_1-1}{p_1}\times\frac{p_2-1}{p_2}\ ...
- Spring - SpringIOC容器详解
一.什么是Spring IOC: Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想. 在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是 ...
- 微服务监控神器Prometheus的安装部署
本文涉及:如何在k8s下搭建Prometheus+grafana的监控环境 基本概念 Prometheus提供了容器和云原生领域数据搜集.存储.处理.可视化和告警一套完整的解决方案,最初时是由Soun ...
- mysql--浅谈子查询1
这是对自己学习燕十八老师mysql教程的总结,非常感谢燕十八老师. 依赖软件:mysql5.6 系统环境:win 子查询概念 子查询就是在原有的查询语句中嵌入新的查询 子查询分类 1.where型子查 ...
- php对数组操作的函数
array_reverse 以相反的顺序返回数组 array_unique 数组元素去重(只对一维数组有效) array_intersect两个或多个数组取交集 implode和explode也 ...
- linux下提示/usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found 解决办法
1.查看gcc版本中包含哪些库. strings /usr/lib64/libstdc++.so.6 | grep GLIBC GLIBCXX_3. GLIBCXX_3.4.1 GLIBCXX_3.4 ...
- Python-15-收集参数
允许用户提供任意数量的参数: def print_params(*params): print(params) >>> print_params('Testing') ('Tes ...
- Jmeter4.0----设置集合点_并发(11)
1.说明 LR中集合点可以设置多个虚拟用户等待到一个点,同时触发一个事务,以达到模拟真实环境下多个用户同时操作,实现性能测试的最终目的. jmeter中使用Synchronizing Timer实现L ...
- Try .NET离线版
https://github.com/dotnet/try Try .NET离线版 使用Try.NET创建可交互.NET文档 原文地址:Create Interactive .NET Docume ...
- ThinkPHP集锦
使用frame搭建页面:不要引入静态的html文件,应该在Action的方法中填写 例:<frame name="menu" src="{:U(GROUP_NAME ...