gpt4 book ai didi

ios - 从两个可变数组获取 NSmutableDictionary 键

转载 作者:行者123 更新时间:2023-11-29 02:08:43 25 4
gpt4 key购买 nike

我有三个可变数组

@interface PickeriviewAndTableViewController ()
//Create mutableArray for tableView

@property(strong,nonatomic)NSMutableArray *msgYear;
@property(strong,nonatomic)NSMutableArray *msgSeason;
@property(strong,nonatomic)NSMutableArray *msgCourse;

我想使用从前两个数组 msgYear[ ]msgSeason[ ] 生成的键将元素添加到数组 msgCourse[ ] .

然后我想使用按钮填充 msgCourse[] 数组

- (IBAction)addCourse:(UIButton *)sender {


//To get value from pickerView, prepare for key and contents

NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc
NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer
NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent];

NSString *num=Number[numRow];
NSString *season=Season[SeaRow];
NSString *course=Course[CourseRow];

NSString *CourseToAdd=[[NSString alloc ]initWithFormat:@"%@ ",course];
NSString *SeasonToAdd=[[NSString alloc ]initWithFormat:@"%@ ",season];
NSString *YearToAdd=[[NSString alloc ]initWithFormat:@"%@ ",num];

// Try to generate key from YearToAdd and SeasonToAdd and fill in content
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];

NSString *keyFromYearAndSeason = @"Don't_know_what_to_do_here";
[contents setObject:[NSArray arrayWithObjects:@"Don't_know_how", nil] forKey:keyFromYearAndSeason];

[keys addObject:keyFromYearAndSeason];

[self setSectionKeys:keys];
[self setSectionContents:contents];

}

我的问题是:如何通过替换这些“不知道如何”的代码行来完成我的代码?

更新:为了更清楚, key 是从 msgYear[ ]msgSeason[ ] 生成的,例如“2001 年秋季”,字典中充满了类(class),例如“化学”、“数学”。

最佳答案

您的其他问题的链接有助于为您试图实现的目标提供背景信息。

您不需要在 addButtton 方法中创建字典或数组 - 您应该将字典作为已经初始化的属性。然后,在添加按钮中检索与键关联的数组 - 如果值为 nil,则创建一个新数组并将类(class)放入。如果值不是 nil,只需将类(class)添加到数组。

@property (strong,nonatomic) NSMutableDictionary *tableEntries;  // alloc/init this in viewDidLoad


- (IBAction)addCourse:(UIButton *)sender {

//To get value from pickerView, prepare for key and contents

NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc
NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer
NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent];

NSString *num=Number[numRow];
NSString *season=Season[SeaRow];
NSString *course=Course[CourseRow];

NSString *key=[NSString stringWithFormat:@"%@ %@",num,season];

NSMutableArray *courses=self.tableEntries[key];
if (courses == nil) {
courses=[NSMutableArray new];
self.tableEntries[key]=courses;
}

[courses addObject:course];

}

NSDictionary 是无序的,因此您需要考虑如何对表中的数据进行排序。您可以像在原始代码中那样使用一个数组来保存键,然后对该数组进行排序。

关于ios - 从两个可变数组获取 NSmutableDictionary 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29502382/

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