我有很多類似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