获取class中属性名称及属性类型

-(void)getTheClassPropertyNameAndAttributes:(NSString *)className withDic:(NSMutableDictionary *)returnDic {

const char *cClassName = [className UTF8String];1

  id theClass = objc_getClass(cClassName);(2)

  unsigned int outCount, i;

  objc_property_t *properties = class_copyPropertyList(theClass, &outCount);*(3)(4)

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

     objc_property_t property = properties[i];

     NSString *propertyNameString = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; 

     NSString *propertyAttributeString = [[NSString alloc] initWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding]; 

NSMutableString *filedTypeString = [NSMutableString stringWithString:propertyAttributeString];

     NSRange range;

     range = [filedTypeString rangeOfString:@","];

     filedTypeString = (NSMutableString *)[filedTypeString substringToIndex:range.location];

     [returnDic setObject:filedTypeString forKey:propertyNameString];

     [propertyNameString release];

     [propertyAttributeString release];

  }

}

(1)UTF8String

  
  

/返回一个null结尾的UTF-8表示的接收器(接收器应该可以理解为c语言中的地址,地址中的内容用UTF-8编码,并以null结尾)

//对应常字符串变量指针(const char *)

Returns a null-terminated UTF8 representation of the receiver.

- (const char *)UTF8String

Return Value

A null-terminated UTF8 representation of the receiver.

Discussion

The returned C string is automatically freed just as a returned object would be released; you should copy the C string if it needs to store it outside of the autorelease context in which the C string is created.

(2) objc_getClass(const char *name)

//返回一个由参数指定的类名的类对象

//参数为一个常字符变量指针

//对于在Objective-C runtime期间没有注册的类返回nil

Returns the class definition of a specified class.

id objc_getClass(const char *name)

Parameters

name

The name of the class to look up.

Return Value

The Class object for the named class, or nil if the class is not registered with the Objective-C runtime.

Discussion

objc_getClass is different from objc_lookUpClass in that if the class is not registered, objc_getClass calls the class handler callback and then checks a second time to see whether the class is registered. objc_lookUpClass does not call the class handler callback.

(3) objc_property_t

//不透明的类型,表示Objective-C声明的属性

objc_property_t

An opaque type that represents an Objective-C declared property.

typedef struct objc_property *objc_property_t;

(4)class_copyPropertyList

//参数 cls 需要检测的类

outCount在返回中存储返回数组长度 当其值为null时返回数组长度为0

//返回一个描述参数类cls中属性的objc_property_t指针数组

//在参数类cls子类中声明的属性不予以返回

//返回的数组需要使用free()函数释放

Describes the properties declared by a class.

objc_property_t * class_copyPropertyList(Class cls, unsigned int *outCount)

Parameters

cls

The class you want to inspect.

outCount

On return, contains the length of the returned array. If outCount is NULL, the length is not returned.

Return Value

An array of pointers of type objc_property_t describing the properties declared by the class. Any properties declared by superclasses are not included. The array contains *outCount pointers followed by a NULL terminator. You must free the array with free().

If cls declares no properties, or cls is Nil, returns NULL and *outCount is 0.

(5)property_getName

//以C字符类型返回属性名字

Returns the name of a property.

const char *property_getName(objc_property_t property)

Return Value

A C string containing the property's name.

(6)- (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding

//返回:NSString object

//参数:cString  一组以null结尾的C字符

  encoding cString的编码方式

Returns an NSString object initialized using the characters in a given C array, interpreted according to a given encoding.

- (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding

Parameters

nullTerminatedCString:

A C array of characters. The array must end with a NULL character; intermediate NULL characters are not allowed.

encoding:

The encoding of nullTerminatedCString.

Return Value

An NSString object initialized using the characters from nullTerminatedCString. The returned object may be different from the original receiver

Discussion

If nullTerminatedCString is not a NULL-terminated C string, or encoding does not match the actual encoding, the results are undefined.

原文:http://tsmc.blog.163.com/blog/static/2018571592012228103849456/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值