iphone - Objective-C:如何获取 `objc_property_t` 属性的值?

标签 iphone objective-c ipad

我使用以下代码遍历对象的所有属性。我成功地将 property name 检索为 char 但我不知道如何获取属于 idproperty value > 类型。关于如何实现这一目标的任何想法?

objc_property_t *allProperties = class_copyPropertyList([currentObject class], &allPropertyCount);

for (unsigned int i = 0; i < allPropertyCount; i++) {

      objc_property_t property = allProperties[i];
      const char * propertyName = property_getName(property);

}

============================================= =========================================

编辑:感谢大家的精彩评论和回答。你们中有些人问我为什么需要这个。好吧,这是原因:

我有几个属于同一类的对象。假设类是 Person,它的实例是 Mary、John 和 David。每个对象的属性设置如下:

mary.age = [NSNumber numberWithInt:20];
john.age = [NSNumber numberWithInt:45];
david.age = [NSNumber numberWithInt:20];

mary.gender = @"female";
john.gender = @"male";
david.gender = @"male";

我的目的是找到一种通用方法来根据给定的属性名称对对象进行分组,例如。这将创建 2 个组 [david 和 mary] 和 [john]:

[self groupBaseDataObjects:self.persons withPropertyName:"age"];

还有这个:

[self groupBaseDataObjects:self.persons withPropertyName:"gender"]; 

还将创建 2 个组 [john 和 david] 和 [mary]

最佳答案

好的——似乎不需要做任何时髦的运行时体操来解决你的问题(别误会我的意思——时髦的运行时体操可能很有趣……但这可能只是浪费时间在这里)。

重述问题:

You have a single class (MyClass) with many instances of that class. The class provides several attributes with instances of the class having many different values for that attribute. You have a collection of instances and you want to easily grab a collection of multiple subsets where the objects in each subset have the same value for a particular attribute.

为此,我将遍历原始问题并执行类似的操作(此代码从未见过编译器;它可能无法编译):

// implement this on MyClass
+ (NSDictionary*)subdivideArrayOfMyClass:(NSArray*) aCollection byAttribute:(NSString*) anAttribute
{
    NSMutableDictionary *collector = [NSMutableDictionary dictionary];
    for (MyClass *anObject in aCollection) {
        id value = [anObject valueForKey: anAttribute];
        // you'd have to special case for nil here if you wanted.
        NSMutableArray *sameValueCollector = [collector objectForKey: value];
        if (!sameValueCollector) {
             sameValueCollector = [NSMutableArray array];
             [collector setObject: sameValueCollector forKey:value];
        }

        [sameValueCollector addObject:anObject];
    }
    return collector; // no need to turn it into an immutable dict
}

这确实假设所有值都可以安全地用作字典中的键。对于您提供的示例,情况确实如此。

如果您需要所有具有单个值的对象,您可以在数组上执行 valueForKey:。如果你愿意,你可以做一些时髦的集合算法来分割对象。但是,这可能会导致对集合进行多次传递,而上面的方法只进行一次传递(尽管收集器中可能会进行大量哈希查找)。直截了当,直到性能分析表明它太慢。

关于iphone - Objective-C:如何获取 `objc_property_t` 属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6784645/

相关文章:

iphone - IOS 如何判断一个数是 NSInteger 还是 float ?

ios - Pod install 不会安装 pod

objective-c - NSLog 替代品?

javascript - 使用业务客户端 ID 将 Google Maps javascript v2 升级到 v3

iphone - 保存太长的录制视频时,应用程序崩溃

iphone - Google Drive SDK - 流式传输音频文件

iphone - 无法验证本地播放器iOS6

iphone - 在 Objective C 中从本地文件夹中读取图像

ios - iPhone SDK : what is the difference between loadView and viewDidLoad?

iphone - 简单的应用程序崩溃?