我有很多类似UILabels
我在代码中设置。 在Objective-C,我会做这样的事情(伪代码):Swift如何在for循环引用
@property (nonatomic, strong) UILabel *l1, *l2, *l3, *l4, *l5, *l6, *l7, *l8, *l9;
...
l1 = [[UILabel alloc] init];
l2 = [[UILabel alloc] init];
l3 = [[UILabel alloc] init];
...
NSArray *lbls = @[l1, l2, l3, l4, l5, l6, l7, l8, l9];
for(UILabel *l in lbls) {
l.textColor = [UIColor redColor];
l.hidden = YES;
...
[self.addSubview:l];
}
我将如何做到这一点的斯威夫特?我如何将参考传递给for in
循环中的变量? 我发现的唯一选择是使用inout
参数创建函数,但将我的代码拆分到不同的区域。
感谢
为什么你有10个标签变量,而不是一个数组变量在第一个地方? – Sulthan
公平点;-)我要实施像vacawama建议 – Joseph