-1
NSString *one = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"id",@"Test1",@"text", nil];
    NSString *two = [NSDictionary dictionaryWithObjectsAndKeys:@"2",@"id",@"Test2",@"text", nil];
    NSString *three = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"id",@"Test3",@"text", nil];

    _options = [NSArray arrayWithObjects:one,two,three,nil];

这是我正在使用并在我的代码中正常运行的代码。现在我想要它动态,比如

int total = 20;
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for(int x = 0; x < total; x++)
    {
        [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"id",@"Test1",@"text", nil]];
    }

但它不起作用。谁能帮我解决这个问题。

主要代码就像

_options = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"id",@"Test1",@"text", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"id",@"Test1",@"text", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"id",@"Test1",@"text", nil],nil];

我想动态创建它,就像值会去 1-100。我怎样才能做到这一点

4

2 回答 2

2

您可能在尝试将原语放入 NSDictionary 时遇到了问题,这是不可能的。将您的值转换为字符串。

for(int x = 1; x <= 100; x++) {
    NSString* object1 = [NSString stringWithFormat:@"%d", x];
    NSString* object3 = [NSString stringWithFormat:@"Test%d", x];
    [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:object1,@"id",object3,@"text", nil]];
}
[_options addObjectsFromArray:arr];
于 2012-11-01T09:02:23.283 回答
1

你应该:

int total = 20;
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    for(int x = 0; x < total; x++)
    {
          NSString *idStr = [NSString stringWithFormat:@"%d", x+1];
          [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys: idStr,@"id",@"Test1",@"text", nil]];
    }
于 2012-11-01T09:02:10.713 回答