Home>

I want to generate UILabel with code instead of StoryBoard.

I want to generate not only one but up to nine.

If i simply generate nine UILabels,

let Label1 = UILabel ()
let Label2 = UILabel ()
let Label3 = UILabel ()
.
.
.
.
let Label9 = UILabel ()

and 9 labels can be generated, but the above method will make the code longer, so I would like to implement it with less code if possible.

I would like to know if there is a way to do it with less processing by loop processing instead of writing as many UILabel declaration statements as necessary.

Please give me an answer.

  • Answer # 1

    let labels = (0 ..<9) .map {_ in UILabel ()}

    will make nine, but handling this Label group seems to be quite tiring.