I've been doing an objective-c program for about 3 years. I started studying swift the other day.
// Declare 3 UILabels
varlbLines = [UILabel] (count: 3, repeatedValue: UILabel())
for i in 0...2 {
// Side-by-side arrangement
lbLines[i].frame = CGRectMake (CGFloat(i*30), 0, 30, 30)
lbLines[i].backgroundColor=UIColor.whiteColor()
self.view.addSubview(lbLines[i])
}
For example, this is how the code looks, but
All three results will be placed in the same location (last edited within frame).
I think there are many ways to reproduce the desired results, but
Is it impossible to display it using an array like this with a swift?
This line of yours:
varlbLines=[UILabel] (count:3, repeatedValue:UILabel())
It's a little hard to understand, but the UILabel()
part that instructs you to generate an instance of UILabel
is evaluated only once.That is, all three references in the array point to the same UILabel
instance.For Objective-C,
UILabel*label=[UILabel alloc] init];
NSArray*lbLines=@[label, label, label];
How does it feel?
If you want to display UILabel
in three places, you must also create three instances.(This is the same for Objective-C and Swift.)
Try changing the first line, for example:
varlbLines= [UILabel(), UILabel(), UILabel()]
If you can start Swift now, it might be better to get Xcode8beta and learn Swift3.(There are many major changes that are not compatible with current Swift 2.2)
© 2024 OneMinuteCode. All rights reserved.