【移动应用开发技术】runtime如何获取属性和成员变量方法_第1页
【移动应用开发技术】runtime如何获取属性和成员变量方法_第2页
【移动应用开发技术】runtime如何获取属性和成员变量方法_第3页
【移动应用开发技术】runtime如何获取属性和成员变量方法_第4页
【移动应用开发技术】runtime如何获取属性和成员变量方法_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】runtime如何获取属性和成员变量方法

这篇文章主要介绍runtime如何获取属性和成员变量方法,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!成员变量1、成员变量的定义Ivar:

实例变量类型,是一个指向objc_ivar结构体的指针

typedef

struct

objc_ivar

*Ivar;2、相关函数//

获取所有成员变量

class_copyIvarList

//

获取成员变量名

ivar_getName

//

获取成员变量类型编码

ivar_getTypeEncoding

//

获取指定名称的成员变量

class_getInstanceVariable

//

获取某个对象成员变量的值

object_getIvar

//

设置某个对象成员变量的值

object_setIvar说明:property_getAttributes函数返回objc_property_attribute_t结构体列表,objc_property_attribute_t结构体包含name和value,常用的属性如下:属性类型name值:Tvalue:变化编码类型name值:C(copy)&(strong)W(weak)空(assign)等value:无非/原子性name值:空(atomic)N(Nonatomic)value:无变量名称name值:Vvalue:变化使用property_getAttributes获得的描述是property_copyAttributeList能获取到的所有的name和value的总体描述,如T@"NSDictionary",C,N,V_dict13、实例应用<!--Person.h文件-->

@interface

Person

:

NSObject

{

NSString

*address;

}

@property(nonatomic,strong)NSString

*name;

@property(nonatomic,assign)NSInteger

age;//遍历获取Person类所有的成员变量IvarList

-

(void)

getAllIvarList

{

unsigned

int

methodCount

=

0;

Ivar

*

ivars

=

class_copyIvarList([Person

class],

&methodCount);

for

(unsigned

int

i

=

0;

i

<

methodCount;

i

++)

{

Ivar

ivar

=

ivars[i];

const

char

*

name

=

ivar_getName(ivar);

const

char

*

type

=

ivar_getTypeEncoding(ivar);

NSLog(@"Person拥有的成员变量的类型为%s,名字为

%s

",type,

name);

}

free(ivars);

}<!--打印结果-->

2016-06-15

20:26:39.412

demo-Cocoa之method

swizzle[17798:2565569]

Person拥有的成员变量的类型为@"NSString",名字为

address

2016-06-15

20:26:39.413

demo-Cocoa之method

swizzle[17798:2565569]

Person拥有的成员变量的类型为@"NSString",名字为

_name

2016-06-15

20:26:39.413

demo-Cocoa之method

swizzle[17798:2565569]

Person拥有的成员变量的类型为q,名字为

_age属性1、属性的定义objc_property_t:声明的属性的类型,是一个指向objc_property结构体的指针

typedef

struct

objc_property

*objc_property_t;2、相关函数//

获取所有属性

class_copyPropertyList

说明:使用class_copyPropertyList并不会获取无@property声明的成员变量

//

获取属性名

property_getName

//

获取属性特性描述字符串

property_getAttributes

//

获取所有属性特性

property_copyAttributeList3、实例应用<!--Person.h文件-->

@interface

Person

:

NSObject

{

NSString

*address;

}

@property(nonatomic,strong)NSString

*name;

@property(nonatomic,assign)NSInteger

age;//遍历获取所有属性Property

-

(void)

getAllProperty

{

unsigned

int

propertyCount

=

0;

objc_property_t

*propertyList

=

class_copyPropertyList([Person

class],

&propertyCount);

for

(unsigned

int

i

=

0;

i

<

propertyCount;

i++

)

{

objc_property_t

*thisProperty

=

propertyList[i];

const

char*

propertyName

=

property_getName(*thisProperty);

NSLog(@"Person拥有的属性为:

'%s'",

propertyName);

}

}<!--打印结果-->

2016-06-15

20:25:19.653

demo-Cocoa之method

swizzle[17778:2564081]

Person拥有的属性为:

'name'

2016-06-15

20:25:19.653

demo-Cocoa之method

swizzle[17778:2564081]

Person拥有的属性为:

'age'应用具体场景1、Json到Model的转化在开发中相信最常用的就是接口数据需要转化成Model了(当然如果你是直接从Dict取值的话。。。),很多开发者也都使用著名的第三方库如JsonModel、Mantle或MJExtension等,如果只用而不知其所以然,那真和“搬砖”没啥区别了,下面我们使用runtime去解析json来给Model赋值。原理描述:用runtime提供的函数遍历Model自身所有属性,如果属性在json中有对应的值,则将其赋值。核心方法:在NSObject的分类中添加方法:-

(instancetype)initWithDict:(NSDictionary

*)dict

{

if

(self

=

[self

init])

{

//(1)获取类的属性及属性对应的类型

NSMutableArray

*

keys

=

[NSMutableArray

array];

NSMutableArray

*

attributes

=

[NSMutableArray

array];

/*

*

例子

*

name

=

value3

attribute

=

T@"NSString",C,N,V_value3

*

name

=

value4

attribute

=

T^i,N,V_value4

*/

unsigned

int

outCount;

objc_property_t

*

properties

=

class_copyPropertyList([self

class],

&outCount);

for

(int

i

=

0;

i

<

outCount;

i

++)

{

objc_property_t

property

=

properties[i];

//通过property_getName函数获得属性的名字

NSString

*

propertyName

=

[NSString

stringWithCString:property_getName(property)

encoding:NSUTF8StringEncoding];

[keys

addObject:propertyName];

//通过property_getAttributes函数可以获得属性的名字和@encode编码

NSString

*

propertyAttribute

=

[NSString

stringWithCString:property_getAttributes(property)

encoding:NSUTF8StringEncoding];

[attributes

addObject:propertyAttribute];

}

//立即释放properties指向的内存

free(properties);

//(2)根据类型给属性赋值

for

(NSString

*

key

in

keys)

{

if

([dict

valueForKey:key]

==

nil)

continue;

[self

setValue:[dict

valueForKey:key]

forKey:key];

}

}

return

self;

}读者可以进一步思考:如何识别基本数据类型的属性并处理空(nil,null)值的处理json中嵌套json(Dict或Array)的处理尝试解决以上问题,你也能写出属于自己的功能完备的Json转Model库。2、快速归档有时候我们要对一些信息进行归档,如用户信息类UserInfo,这将需要重写initWithCoder和encodeWithCoder方法,并对每个属性进行encode和decode操作。那么问题来了:当属性只有几个的时候可以轻松写完,如果有几十个属性呢?那不得写到天荒地老.原理描述:用runtime提供的函数遍历Model自身所有属性,并对属性进行encode和decode操作。核心方法:在Model的基类中重写方法:-

(id)initWithCoder:(NSCoder

*)aDecoder

{

if

(self

=

[super

init])

{

unsigned

int

outCount;

Ivar

*

ivars

=

class_copyIvarList([self

class],

&outCount);

for

(int

i

=

0;

i

<

outCount;

i

++)

{

Ivar

ivar

=

ivars[i];

NSString

*

key

=

[NSString

stringWithUTF8String:ivar_getName(ivar)];

[self

setValue:[aDecoder

decodeObjectForKey:key]

forKey:key];

}

}

return

self;

}-

(void)encodeWithCoder:(NSCoder

*)aCoder

{

unsigned

int

outCount;

Ivar

*

ivars

=

class_copyIvarList([self

class],

&outCount);

for

(int

i

=

0;

i

<

outCount;

i

++)

{

Ivar

ivar

=

ivars[i];

NSString

*

key

=

[NSString

stringWithUTF8String:ivar_getName(ivar)];

[aCoder

encod

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论