swift 如何在数组中追加 UIColor

swift how to append UIColor in array

我的代码是:

public var color = [UIColor]()
color.append(String(UIColor(dragInViews[i]!.backgroundColor)))

此代码有错误: Argument labels '(_:)' do not match any available overloads.

我正在尝试解决问题,但我不知道。问题是什么 如何解决我的问题?

您需要创建新的 UIColor 类型数组,并且您需要在其中附加颜色值而不是字符串类型直接颜色

var arrColor = [UIColor]()
arrColor.append(UIColor(dragInViews[i]?.backgroundColor ?? UIColor()))

您不需要 String() 部分(也不需要 UIColor() 初始值设定项),它已经是 UIColor 并且数组定义为 UIColor 的数组, 所以只需附加它就足够了。

public var color = [UIColor]()
color.append(dragInViews[i]!.backgroundColor)

请注意 UIView backgroundColor 属性 已经是一种颜色,因此没有必要再次实例化它。

您看到的那个特定错误是因为您试图以这种方式 UIColor(something) 用它的初始值设定项来实例化颜色,但存在的初始值设定项是 UIColor(white:, alpha:) 之间的其他值。查看文档 here.