这是我为字典中的相同键返回唯一值的代码。现在,在我的日志中,我的“对象数组:”是 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);
}
第一个日志打印出三个唯一值和键。第二个仅打印单个键的单个值(这是我想要的......但我需要所有三个键值)所以我现在的问题是我无法从字典中为每个唯一值提取一个键并添加它到我的titlesArray。
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];