Runtime(二)
2016-04-12 21:58
274 查看
一些公用类:
@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
@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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Java Runtime Environment 5.0 Update 12 下载
- php set_magic_quotes_runtime() 函数过时解决方法
- Asp.Net 程序错误Runtime Error原因与解决
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- System 类 和 Runtime 类的常用用法介绍
- .net平台推送ios消息的实现方法