【问题标题】:Varying number of NSArrays to fill要填充的不同数量的 NSArray
【发布时间】:2014-09-28 06:38:07
【问题描述】:

我正在开发一个需要一点帮助的应用程序,

基本上我正在从数据库中创建和填充一些数组和字典,但是到目前为止,数组和字典的数量没有改变,我现在需要能够拥有不同的数字(不超过 8 个 - 这是我一直在使用的东西)

我只是不确定如何只处理一定数量的请求,有人可以帮忙吗?

下面的代码是我当前正在运行的代码,因此它可以工作,但我需要能够仅在 1 和 8 之间填充/创建,而不仅仅是我的标准 8。

NSMutableArray *selections = [[NSMutableArray alloc] init];
for (NSIndexPath *indexPath in arSelectedRows) {
    [selections addObject:[tableDataSource objectAtIndex:indexPath.row]];
}

NSArray *Category1 = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:0] valueForKey:@"_id"]];
NSArray *Category2 = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:1] valueForKey:@"_id"]];
NSArray *Category3 = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:2] valueForKey:@"_id"]];
NSArray *Category4 = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:3] valueForKey:@"_id"]];
NSArray *Category5 = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:4] valueForKey:@"_id"]];
NSArray *Category6 = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:5] valueForKey:@"_id"]];
NSArray *Category7 = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:6] valueForKey:@"_id"]];
NSArray *Category8 = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:7] valueForKey:@"_id"]];

NSDictionary *cat1 = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:0] valueForKey:@"_id"]];
NSDictionary *cat2 = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:1] valueForKey:@"_id"]];
NSDictionary *cat3 = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:2] valueForKey:@"_id"]];
NSDictionary *cat4 = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:3] valueForKey:@"_id"]];
NSDictionary *cat5 = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:4] valueForKey:@"_id"]];
NSDictionary *cat6 = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:5] valueForKey:@"_id"]];
NSDictionary *cat7 = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:6] valueForKey:@"_id"]];
NSDictionary *cat8 = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:7] valueForKey:@"_id"]];

DBAdaptor 是我的数据库类文件。 selections 包含需要创建的数组/字典的数量。

任何帮助都会很棒!

谢谢。

【问题讨论】:

  • 这是使用(惊喜!)数组的地方。

标签: ios objective-c arrays dictionary nsarray


【解决方案1】:

假设你有不同的数字;

int count;

您可以遍历一个循环,例如; (另外,您可能希望保存这些数组和字典)

NSMutableArray* arrayHolder = [NSMutableArray new];
NSMutableArray* categoryHolder = [NSMutableArray new];

for(int i = 0 ; i < count ; i++)
{
    NSArray* newArray = [DBAdapter GetQuestionsfromid:[[selections objectAtIndex:i] valueForKey:@"_id"]];
    NSDictionary* newCategory = [DBAdapter GetCategoryDictionary:[[selections objectAtIndex:i] valueForKey:@"_id"]];

    [arrayHolder addObject:newArray];
    [categoryHolder addObject:newCategory];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多