-(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
(2) objc_getClass(const char *name)/返回一个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.
(3) objc_property_t//返回一个由参数指定的类名的类对象
//参数为一个常字符变量指针
//对于在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.
(4)class_copyPropertyList//不透明的类型,表示Objective-C声明的属性
objc_property_t
An opaque type that represents an Objective-C declared property.
typedef struct objc_property *objc_property_t;
(5)property_getName//参数 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.
//以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
获取class中属性名称及属性类型
最新推荐文章于 2024-05-10 10:17:55 发布