gpt4 book ai didi

ios - 从NSMutableDictionary中提取一个值

转载 作者:行者123 更新时间:2023-12-01 19:06:18 27 4
gpt4 key购买 nike

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

最佳答案

看起来有一些类型错误

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

它应该是
 PFObject *key = [obj valueForKey:@"titleLabel"];

关于ios - 从NSMutableDictionary中提取一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19461884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com