我有一个对象(图像)数组,其键值为@“titleLabel”,存储在后端服务器上(解析)。我想要做的是在pickerView中显示标题列表,但我只想为每个键匹配的@“titleLabels”组显示一个标题。例如:如果我有五个带有titleLabel键的对象(图像):'Spring Break'我只想显示标题'Spring Break'的一个实例而不是全部五个。我写的代码是我解决它的最好的业余镜头但是我的NSLog''titles array count'返回值为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];
答案 0 :(得分:0)
PFObject *object = [self.objectsArray valueForKey:@"titleLabel"];
if(object != [self.objectsArray valueForKey:@"titleLabel"]){
self.titlesArray = [self.titlesArray arrayByAddingObject:object];
}
您正在检查对象是否等于您刚分配的对象。此外,你应该使用NSMutableArray,但如果你不在乎,我也不在乎。 :)说,我猜你的其他循环应该看起来像这样
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]);
}