1

我有一个对象(图像)数组,其键值为@“titleLabel”,存储在后端服务器(解析)上。我想做的是在pickerView中显示标题列表,但我只想为每组键匹配的@“titleLabels”显示一个标题。例如:如果我有五个带有 titleLabel 键的对象(图像):“春假”,我只想显示标题“春假”的一个实例,而不是全部五个。我编写的代码是我最好的业余解决方案,但我的“标题数组计数”的 NSLog 返回值 1,当我记录该值时,我再次获得所有标题。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    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 {
           NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        for(id obj in self.objectsArray){
            PFObject *key = [self.objectsArray valueForKey:@"titleLabel"];
            if(![dict objectForKey:key]){
                [dict setValue:obj forKey:key];
            }
        }
        for (id key in dict) {
        NSLog(@"key: %@, value: %@ \n", key, [dict objectForKey:key]);
        NSLog(@"Objects array is %d", [self.objectsArray count]);
        NSLog(@"Titles array is %d", [self.titlesArray count]);
        }
        [self.pickerView reloadComponent:0];
        }
    }]; 

这是我在一个单独的控制器中定义我的键的地方:

 PFObject *image = [PFObject objectWithClassName:@"Images"];
        [image setObject:self.releaseDate forKey:@"releaseDate"];
        [image setObject:file forKey:@"file"];
        [image setObject:fileType forKey:@"fileType"];
        [image setObject:title forKey:@"titleLabel"];
        [image setObject:deadline forKey:@"deadline"];
        [image setObject:self.recipients forKey:@"recipientIds"];
        [image setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];
        [image setObject:[[PFUser currentUser] username] forKey:@"senderName"];
        [image saveInBackground];
4

1 回答 1

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

if(object != [self.objectsArray valueForKey:@"titleLabel"]){
      self.titlesArray = [self.titlesArray arrayByAddingObject:object];

}

您正在检查对象是否等于您刚刚分配的对象。此外,您应该使用 NSMutableArray 但如果您不在乎,我也不在乎。:) 这么说,我猜你的 else 循环应该是这样的

    self.objectsArray = objects;

    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    for(id obj in self.objectsArray){
        PFObject *key = [self.objectsArray valueForKey:@"titleLabel"];
        if(![dict objectForKey:key]){
            [dict setValue:obj forKey:key];
        }
    }
    for (id key in dict) {
        NSLog(@"key: %@, value: %@ \n", key, [dict objectForKey:key]);
    }
于 2013-10-17T00:19:46.237 回答