iphone - 如何在for循环中创建NSArray

标签 iphone objective-c ios cocoa-touch sdk

如何通过在for循环中执行以下代码来缩短代码?

NSArray *images = [NSArray arrayWithObjects:
                         [UIImage imageNamed:@"z1.png"],
                         [UIImage imageNamed:@"z2.png"],
                         [UIImage imageNamed:@"z3.png"],
                         [UIImage imageNamed:@"z4.png"],
                         [UIImage imageNamed:@"z5.png"],
                         [UIImage imageNamed:@"z6.png"],
                         [UIImage imageNamed:@"z7.png"],
                         [UIImage imageNamed:@"z8.png"],
                         [UIImage imageNamed:@"z9.png"],
                         [UIImage imageNamed:@"z10.png"],
                         [UIImage imageNamed:@"z11.png"],
                         [UIImage imageNamed:@"z12.png"],
                         [UIImage imageNamed:@"z13.png"],
                         [UIImage imageNamed:@"z14.png"],
                         [UIImage imageNamed:@"z15.png"],
                         [UIImage imageNamed:@"z16.png"],
                         [UIImage imageNamed:@"z17.png"],
                         [UIImage imageNamed:@"z18.png"],
                         [UIImage imageNamed:@"z19.png"],
                         nil];

最佳答案

神奇之处在于NSMutableArray中的注释中提到的@Teak。实际上,所有容器类都有一个可变版本,例如NSStringNSDictionaryNSSet等。

NSMutableArray *images = [NSMutableArray array];

for (NSUInteger i = 1; i <= 19; i++) {
    NSString *imageName = [NSString stringWithFormat:@"z%d.png", i];
    [images addObject:[UIImage imageNamed:imageName];
}

关于iphone - 如何在for循环中创建NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11276982/

相关文章:

iphone - applicationWillResignActive x 屏幕保护程序

iphone - 声音引擎泄漏……预加载是一种不好的做法吗?

ios - Alamofire .post API 错误 : Code=3840 "Invalid value around character 1

ios - 如何在 Root View 中隐藏导航 Controller ?

ios - 使用未解析的标识符 'objectAtIndexPath'

iphone, ipad 复制 UIView - 克隆 View

ios - “Static member ' … ' cannot be used on instance of type ' … '”

iphone - 如何确定键盘是否隐藏了 subview ......?

objective-c - 无法链接两个tableView的数据源

ios - UIDatePicker 作为 inputView