ios获取类的属性,获取CLPlacemark类所有属性,Declaration of 'objc_property_t' must be imported from module 'objecti

本文介绍了一种通过遍历CLPlacemark类的所有属性并获取其值的方法,以确定哪些属性适合用于显示地理位置信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做的项目中,定位后获得的

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]]);
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

启程Boy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值