-2

I want my swift code to create buttons using colors. I know I want to declare array but some of the things I have tried are not working. What I tried below is not working. I have tried to do various forms of wrapping but it did not work. The goal of this code is to not use any storyboards and do everything by code.

  var red,blue = UIButton()
Sam Burns
  • 65
  • 1
  • 10
  • What do you mean by "not working"? What exactly do you expect to happen? – Sweeper Apr 26 '21 at 04:53
  • I am getting a error Type annotation missing in pattern – Sam Burns Apr 26 '21 at 04:54
  • "What I tried below is not working" That's because there are rules in declaring a variable, and you don't abide by them. – El Tomato Apr 26 '21 at 04:57
  • An array declaration looks like this `var buttons = [UIButton]()`. You are declaring two variables `red` and `blue`, one of which has no type. Anyway, what do you want to do with the array? Closing as unclear. – Sweeper Apr 26 '21 at 04:58
  • Could you add more code and explain more clearly. I'm not sure what you're trying to say. – KyawTheMonkey Apr 26 '21 at 05:03

1 Answers1

0

One compact way to declare two variables in one line is to use tuple notation:

let (red,blue) = (UIButton(), UIButton())

Perhaps that's what you're asking for. But there is no array in the story; it's hard to see why you mention arrays in the first place.

On the other hand, if you really do want an array as you claim, then it's hard to see what the names red and blue are for, since the elements of an array do not have names (with regard to the array). You could make an array of two new buttons by saying:

let arrayOfTwoButtons = (0..<2).map {_ in UIButton()}
matt
  • 515,959
  • 87
  • 875
  • 1,141