最近在做的项目中,定位后获得的
调用的时候打印属性值:
CLPlacemark类不知道用哪个属性的值来显示地理位置,所有调试的时候用了获取类的所有属性,再获取属性对应的值。一看就明白用哪个属性显示适合
应该特别注意的是在使用 objc_property_t 时先导入头文件 #import<objc/runtime.h>,否则报错:Declaration of 'objc_property_t' must be imported from module 'objectiveC.runtime'before it is required.
/**
* 获取 类的属性。
*
* @param class 传入的类,其他类的属性方法类似
*
* @return 返回存放属性的数组
*/
- (NSArray *)getClassAttribute:(id)class
{
unsigned int count;
objc_property_t *properties = class_copyPropertyList([class class], &count);
NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
for(int i = 0; i < count; i++)
{
/*
// 分步取属性
objc_property_t property = properties[i];
NSString *name = [NSString stringWithUTF8String:property_getName(property)];
NSString *attributes = [NSString stringWithUTF8String:property_getAttributes(property)];
NSLog(@"name0000:%@",name);
NSLog(@"attributes0000:%@",attributes);
*/
const char *propertyName = property_getName(properties[i]);
[propertiesArray addObject: [NSString stringWithUTF8String:propertyName]];
}
free(properties);
return propertiesArray;
}
调用的时候打印属性值:
// 如 获取 CLPlacemark 类 使用的时候 这样使用
CLPlacemark *placeMark = [[CLPlacemark alloc]init];
NSArray *attributeArr = [self getClassAttribute:placeMark];
for (int tt = 0; tt < attributeArr.count; tt ++)
{
// 打印值 使用 valueForKey:
NSLog(@"placeMark.%@:%@", attributeArr[tt], [placeMark valueForKey:attributeArr[tt]]);
}