Objective-C(iOS)变量替换数组值

时间:2013-03-06 22:12:08

标签: ios objective-c nsarray

如何编写NSArray值以使用与此NSString类似的变量:

Levelname=[NSString stringWithFormat:@"Background1Level%d.png",LevelNO];
    [backgroundimagview setImage:[UIImage imageNamed:Levelname]];

以下是我需要转换为使用LevelNO变量的代码块:

        [character setImage:[UIImage imageNamed:@"character1_09.png"]];
        character.animationImages=[NSArray arrayWithObjects:
                              [UIImage imageNamed:@"character1_01.png"],
                              [UIImage imageNamed:@"character1_02.png"],
                              [UIImage imageNamed:@"character1_03.png"],
                              [UIImage imageNamed:@"character1_04.png"],nil ];

1 个答案:

答案 0 :(得分:4)

使用整数循环遍历它们而不是“LevelNO”使用该整数。像这样:

for(int i = 0; i < {number of levels here}, i++)
{
  Levelname=[NSString stringWithFormat:@"Background1Level%d.png",i];
  [myArray addObject:[UIImage imageWithName:Levelname];
}

确保保留一个数组并替换myArray