IOS Runtime(二)
2015-11-25 14:32
441 查看
IOS高级开发~Runtime(一)
IOS高级开发~Runtime(二)
IOS高级开发~Runtime(三)
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
IOS高级开发~Runtime(二)
IOS高级开发~Runtime(三)
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
相关文章推荐
- iOS 运行流程
- IOS Runtime(一)
- iOS 如何调试第三方统计到的崩溃报告
- IOS开发使用KeychainItemWrapper 持久存储用户名和密码
- iOS boundingRectWithSize使用
- iOS 项目开发使用方法
- iOS开发-Protocol协议及委托代理(Delegate)传值
- IOS中的协议与代理
- iOS高级开发——CollectionView的cell中按钮的点击实现
- 苹果开发者相关
- iOS 屏幕旋转控制(iOS 9.1适用)
- @(),@[],@{}
- iOS学习之frame和bounds
- ios初体验,终端命令
- iOS时间戳与日期互转
- iOS导航标题不居中显示问题
- iOS 计算代码块执行时间
- ios中tabbar得title和navigationbar的title如何修改
- iOS framework 合并真机和模拟器run Scripte
- iOS依赖注入