我有一个对象(图像)数组,其键值为@“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];