Search code examples
iosswift2nsmutablearrayuicolor

Create an array of UIColor in swift


Why is this giving me an error?

 var colorArray = [UIColor.self]

colorArray.append(UIColor.redColor()) //error here
colorArray.removeLast() 

how do I define a mutable array to store UIColor?


Solution

  • @George Asda:

    The error coming up is "Cannot convert value type 'UIColor' to expected argument type 'UIColor.Type'"

    The error disappears when an empty array of type UIColor is set up e.g. below I have replicated it...

    var uiColorArray = [UIColor]()   // Empty Array of type UIColor
    

    Subsequently, when I append and removeLast like in your example and print at each stage, all appears fine without any error... e.g. inside a Func...

    self.uiColorArray.append(UIColor.blueColor) // in Swift 3, its UIColor.blue
        print(uiColorArray)
    
        self.uiColorArray.removeLast()
        print(uiColorArray)
    

    Error is gone.