gpt4 book ai didi

ios - 具有不同记录和 UIPickerView 的 NSFetchRequest

转载 作者:行者123 更新时间:2023-12-01 16:54:27 25 4
gpt4 key购买 nike

我正在尝试填充 UIPickerView提取结果。我能够做到。由于有重复,我决定使用下面的代码来获取不同的记录和只需要的属性。代码工作正常,数组 array1 & dictlevel1有正确的数据(如下所示)。我不知道如何获得 array1进入选择器 View ?

    NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Factors" inManagedObjectContext:managedObjectContext];

[fetchRequest setEntity:entity];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setPropertiesToFetch:@[@"level1"]];

self.title = @"Factors";
array1 = [[NSArray alloc] init];
array1 = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSDictionary *dictlevel1 = [array1 dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"level1"]];

NSLog(@"%@", array1[1]);
NSLog(@"%@", [dictlevel1 dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"level1"]]);

这是 NSLog 的输出:
2012-10-23 14:55:47.774 CoreData2[1477:c07] (
{
level1 = "External Combustion Boilers";
},
{
level1 = "Internal Combustion Engines";
},
{
level1 = "Industrial Processes";
},
{
level1 = "Petroleum and Solvent Evaporation";
},
{
level1 = "Waste Disposal";
},
{
level1 = "Stationary Source Fuel Combustion";
},
{
level1 = "Miscellaneous Area Sources";
},
{
level1 = "Mobile Sources";
},
{
level1 = "Solvent Utilization";
},
{
level1 = "Storage and Transport";
},
{
level1 = "Waste Disposal, Treatment, and Recovery";
},
{
level1 = "Natural Sources";
}
)

2012-10-23 14:55:47.775 CoreData2[1477:c07] {
level1 = (
"External Combustion Boilers",
"Internal Combustion Engines",
"Industrial Processes",
"Petroleum and Solvent Evaporation",
"Waste Disposal",
"Stationary Source Fuel Combustion",
"Miscellaneous Area Sources",
"Mobile Sources",
"Solvent Utilization",
"Storage and Transport",
"Waste Disposal, Treatment, and Recovery",
"Natural Sources"
);
}

最佳答案

我建议使用这样的代码片段来获得结果字典中给定键(例如 endCalYear)的不同结果(@distinctUnionOfObjects):

NSArray *distinctYears = [periodsFilteredByDates
valueForKeyPath:@"@distinctUnionOfObjects.endCalYear"];

pickerView:numberOfRowsInComponent:然后您可以使用 distinctYears.count 返回行数在 pickerView:titleForRow:forComponent:您可以使用这样的代码片段为每个标题返回一个字符串:
NSNumber *endCalYear = distinctResults[row];
NSString *rowTitle = [NSString stringWithFormat:@"%@",endCalYear];
return rowTitle;

关于ios - 具有不同记录和 UIPickerView 的 NSFetchRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13038410/

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