0

So the only way i can think of achieving this is by putting the array inside mainArray into a variable and then indexing that. Is there an easier way?

  mainArray          = [ 3400, "Overwatch", [UIButton(), UIButton()]] // Some buttons already made
  currentButtonArray = mainArray[mainArray.count - 1] as! NSArray

    for i in 0..<currentButtonArray.count {
        buttonArray.append( currentButtonArray[i] as! UIButton)
    }
4
  • 1
    This is likely an inappropriate data structure – what exactly are you using this array for? Commented Oct 2, 2016 at 13:58
  • @Hamish My goal is every now and then i would check the main array if it added buttons (Which it will), and disable userInteraction for those newly added buttons (After being pressed) by looping through the buttonArray Commented Oct 2, 2016 at 14:04
  • 1
    But what does the contents of mainArray represent? Integers, Strings and [UIButton]s aren't related. Commented Oct 2, 2016 at 14:18
  • @Hamish imagine going through a park and every sign you read you want to add it to your bag ("String") and every now and then you get to cross roads and you have to choose a button ("Buttons"). Integers not really using them Commented Oct 4, 2016 at 13:30

2 Answers 2

2

If there is one array containing only UIButton instances, just filter it.

let mainArray : [Any] = [3400, "Overwatch", [UIButton(), UIButton()]]
if let buttonArray = mainArray.filter({$0 is [UIButton]}).first as? [UIButton] {
  print(buttonArray)
}

or

let buttonArray = Array(mainArray.flatMap{$0 as? [UIButton]}.joined())

The second approach returns a non-optional empty array if there is no array of UIButton in mainArray

Sign up to request clarification or add additional context in comments.

3 Comments

Or you could do mainArray.flatMap({$0 as? [UIButton]}).first ;) If there's more than one array, you could do a .lazy.flatMap in order to ensure that it stops iterating upon finding the first [UIButton].
There will be more than one array of buttons. Will i take the same approach?
What result do you expect? The result of the flatMap solution is one array containing all buttons. The filter solution returns only the first array.
1

If the subarray is of all one type, you can append all in one go:

var buttonArray = [UIButton]()
let mainArray:[Any] = [3400, "Overwatch", [UIButton(), UIButton()]] // Some buttons already made
if let currentButtonArray = mainArray.last as? [UIButton] {
   buttonArray.append(contentsOf: currentButtonArray)
}

Or you could simply write:

guard let currentButtonArray = mainArray.last as? [UIButton] else {
    // do something e.g. return or break
    fatalError()
}
// do stuff with array e.g. currentButtonArray.count

If you didn't know the position in the array of the nested UIButton array or if there were multiple nested button arrays then this would work:

let buttonArray = mainArray.reduce([UIButton]()){ (array, element) in if let bArray = element as? [UIButton] {
        return array + bArray
    }
    else {
         return array
    }
}

Note: this is Swift 3 code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.