IOS遍历未知对象属性、函数

转:http://blog.csdn.net/chaoyuan899/article/details/24399761

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

#import <objc/runtime.h>

 

 

@implementation
NSObject (PropertyListing)  

 

/* 获取对象的所有属性 */

- (NSDictionary
*)properties_aps

{

    NSMutableDictionary
*props = [NSMutableDictionary
dictionary];  

   unsigned int
outCount, i;  

   objc_property_t *properties = class_copyPropertyList([self
class], &outCount);  

   for
(i = 0; i<outCount; i++)

    {

       objc_property_t property = properties[i];

       const
char* char_f =property_getName(property);

       NSString
*propertyName = [NSString
stringWithUTF8String:char_f];

       id
propertyValue = [self
valueForKey:(NSString
*)propertyName];  

       if
(propertyValue) [props setObject:propertyValue forKey:propertyName];  

    }  

   free(properties);  

   return
props;  

}  

 

/* 获取对象的所有方法 */

 

-(void)printMothList

{

   unsigned int
mothCout_f =0;

   Method* mothList_f = class_copyMethodList([self
class],&mothCout_f);

   for(int
i=0;i<mothCout_f;i++)

    {

       Method temp_f = mothList_f[i];

       IMP imp_f = method_getImplementation(temp_f);

       SEL
name_f = method_getName(temp_f);

       const
char* name_s =sel_getName(method_getName(temp_f));

       int
arguments = method_getNumberOfArguments(temp_f);

       const
char* encoding =method_getTypeEncoding(temp_f);

       NSLog(@"方法名:%@,参数个数:%d,编码方式:%@",[NSString
stringWithUTF8String:name_s],

                                            arguments,

                            [NSString
stringWithUTF8String:encoding]);

    }

   free(mothList_f);

}

 

 

@end

  

IOS遍历未知对象属性、函数,布布扣,bubuko.com

时间: 2024-07-30 13:51:54

IOS遍历未知对象属性、函数的相关文章

IOS 遍历未知对象的属性和方法

/* 注意:要先导入ObjectC运行时头文件,以便调用runtime中的方法*/ #import <objc/runtime.h> @implementation NSObject (PropertyListing) 1./* 获取对象的所有属性,不包括属性值 */ - (NSArray *)getAllProperties { u_int count; objc_property_t *properties  =class_copyPropertyList([self class], &a

遍历js对象属性

<script type="text/javascript"> var p = { a:'a', e:{ a:'a', c:'c' }, b:'1', c:{ a:2, x:'fffff', y:{ ee:'fff', f:200 } } } ; //遍历js对象属性 var ObjInfo = { /** @param obj 要遍历的对象 @param placeholder 占位符,排版用,如空格 */ info : function(obj,placeholder)

js之oop &lt;二&gt; 对象属性

js中对象属性可以动态添加和删除.删除对象属性用delete关键字. function obj(){ } var oo = new obj(); oo.a = "a"; oo.b = "b"; oo.a; //输出a oo.b; //输出b delete oo.a; oo.a; //输出undefined 在js中对象的属性以键值对的方式来体现,且(在遍历时)无序.js中属性名(也就是key)可以用点来访问(或赋值),也可以用中括号key值来访问(或赋值)(用中括号

对象属性的可枚举性

JavaScript中对象的属性分为两种:数据属性和访问器属性.然后根据具体的上下文环境的不同,又可以将属性分为:原型属性和实例属性.原型属性是定义在对象的原型(prototype)中的属性,而实例属性一方面来自构造的函数中,然后就是构造函数实例化后添加的新属性. 在JavaScript中除了检测对象的属性是否存在,还会经常对对象的属性进行遍历(枚举).而在JavaScript中遍历一个对象的属性并不太简单,主要有两个原因: JavaScript中的对象通常都处在某个原型链中,它会从一个或多个的

高性能 javaScript 之遍历对象属性

在面向对象的开发工作中,经常会遇到检查对象属性和遍历对象属性的情况. JavaScript 不包含如 java.C 等语言的传统类继承模型,而是使用 prototype 原型模型. JavaScript 原型链继承中属性查找过程 当查找一个对象的属性时,JavaScript 会向上遍历原型链,直到找到给定名称的属性为止.到查找到达原型链的顶部 - 也就是 Object.prototype - 但是仍然没有找到指定的属性,就会返回 undefined. 使用 for in 循环可以遍历对象所有的属

js对象-属性遍历

为了加强js对象的认知,又刷了一题. 直接上题目: 找出对象 obj 不在原型链上的属性(注意这题测试例子的冒号后面也有一个空格~)1.返回数组,格式为 key: value2.结果数组不要求顺序 示例: //输入 var C = function() {this.foo = 'bar'; this.baz = 'bim';}; C.prototype.bop = 'bip'; iterate(new C()); //输出 ["foo: bar", "baz: bim&quo

对象属性操作-ios

#import <Foundation/Foundation.h> @class Author; @interface Books : NSObject{ @private NSString *color; int book_id; NSString *box; Author *author; NSArray *relationbook; float price; @public NSString *name; } @property (nonatomic) int size; -(void)

JS/jQuery 遍历对象属性

Javascript For/In 循环: 循环遍历对象的属性 var person={fname:"John",lname:"Doe",age:25}; for (x in person) { txt=txt + person[x]; } 结果:JohnDoe25 jQuery jQuery.each() 遍历对象属性 var arr = ["one", "two", "three", "fou

201506230818_《JavaScript权威指南(第六版)——callee和caller、对象属性用作实参、自定义函数属性》(P175-180)

1. callee 正在执行的函数.使用方法:arguments.callee... caller 正在调用执行函数的函数. 2.对象属性用作实参,形如:function fn(arg) { var arg.name = name || 'Josn', arg.age = age || 60 , ... } 3. 在传入实参时候,宁愿在检查值类型时报错也不要在执行时候报错; 4. if(element == null) continue;  //过滤null或undefined元素; 5.  i