从NSMutableDictionary中提取值

时间:2013-10-19 03:01:44

标签: ios nsmutabledictionary key-value

这是我为字典中相同键返回唯一值的代码。现在,在我的日志中,我的“对象数组:”是6(3组(2个具有相同键的对象)),而我的“字典:”返回每组中1个对象的值(3个唯一值)。在我的'for'声明中:

for (id key in dict)
{
       self.titlesArray = [NSMutableArray arrayWithObject:dict];
       NSLog(@"titles: %@", self.titlesArray);
       self.titlesArray = [[NSMutableArray alloc] initWithObjects:[dict valueForKey:key] ,nil];
       NSLog(@"titles: %@", self.titlesArray);

}

第一个日志打印出三个唯一值AND键。第二个只为单个键打印一个值(这就是我想要的......但是我需要所有三个键值)所以现在我的问题是我无法为字典中的每个唯一值拉一个键并添加它我的头衔.Array。

for (id key in dict) 
{
    self.titlesArray = [NSMutableArray arrayWithObject:dict];
    self.titlesArray = [[NSMutableArray alloc] initWithObjects:[dict valueForKey:key] ,nil];

代码不太正确。

    PFQuery *query = [PFQuery queryWithClassName:@"Images"];
    [query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        else {

            // found messages!
            self.objectsArray = objects;

            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            for(id obj in self.objectsArray){
                PFObject *key = [obj valueForKey:@"titleLabel"];

                if(![dict objectForKey:key]){
                    [dict setValue:key forKey:[obj valueForKey:@"titleLabel"]];
                }
            }
            for (id key in dict) {
                self.titlesArray = [NSMutableArray arrayWithObject:dict];
NSLog(@"titles: %@", self.titlesArray);
                self.titlesArray = [[NSMutableArray alloc] initWithObjects:[dict valueForKey:key] ,nil];
NSLog(@"titles: %@", self.titlesArray);
            } 
            NSLog(@"dictionary: %@", dict);
            NSLog(@"Objects array is %d", [self.objectsArray count]);

            [self.pickerView reloadComponent:0];

4 个答案:

答案 0 :(得分:1)

看起来行

中存在一些类型错误
PFObject *key = [self.objectsArray valueForKey:@"titleLabel"];

应该是

 PFObject *key = [obj valueForKey:@"titleLabel"];

答案 1 :(得分:1)

你的PFObject是怎样的。它有字符串吗?根据你的问题,你已经知道你不能将PFObject作为字典的密钥传递。如果您的对象是这样的

interface PFObject : NSObject

{
    NSString *keyString;
    ......
    .Some other variables
}

然后您应该像这样使用它将其设置为键

PFObject *key = [self.objectsArray valueForKey:@"titleLabel"];

            if(![dict objectForKey:@"titleLabel"]){
                [dict setValue:obj forKey:[key valueForKey@"titleLabel"]];
            }

答案 2 :(得分:1)

这是在这一行发生的,不是吗:

 if(![dict objectForKey:@"titleLabel"]){
            [dict setValue:obj forKey:key];
        }
    }

您将“obj”设置为值,没有问题,但是您使用的是“密钥”,这是一个PFObject,但NSDictionary需要一个NSString作为密钥。

如果PFObject包含您要使用的NSString属性,则可以将其传入。例如,如果PFObject具有名为“name”的NSString属性,则可以调用它:

if(![dict objectForKey:@"titleLabel"]) {
           [dict setValue:obj forKey:key.name];
        }
    }

需要注意的是NSMutableDictionary定义此方法时的参数类型,即(NSString *):

 - (void)setValue:(id)value forKey:(NSString *)key

答案 3 :(得分:0)

这是我发现的代码。它通过键对数组进行排序,只返回特定键的唯一值:

PFQuery *query = [PFQuery queryWithClassName:@"Images"];
    [query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
    [query orderByDescending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
        else {
            // found messages!
            self.objectsArray = objects;

            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            for(id obj in self.objectsArray){
                PFObject *key = [obj valueForKey:@"titleLabel"];

                if(![dict objectForKey:key]){
                    [dict setValue:key forKey:[obj valueForKey:@"titleLabel"]];
                }
            }
            for (id key in dict) {
                self.titlesArray = [NSMutableArray arrayWithObject:dict];
                [self.titlesArray addObject:dict.allKeys];
                self.titlesArray = [self.titlesArray objectAtIndex:1];
            }
            NSLog(@"titles: %@", self.titlesArray);

            [self.pickerView reloadComponent:0];