我已将此代码写入Swift,但最后一行代码返回此错误消息:
无法调用非功能类型的值' [UIColor]'
import Foundation
import UIKit
struct Colores {
let colores = [UIColor(red: 210/255.0, green: 90/255.0, blue: 45/255.0, alpha: 1),
UIColor(red: 40/255.0, green: 170/255.0, blue: 45/255.0, alpha: 1),
UIColor(red: 3/255.0, green: 180/255.0, blue: 90/255.0, alpha: 1),
UIColor(red: 210/255.0, green: 190/255.0, blue: 5/255.0, alpha: 1),
UIColor(red: 120/255.0, green: 120/255.0, blue: 50/255.0, alpha: 1),
UIColor(red: 130/255.0, green: 80/255.0, blue: 90/255.0, alpha: 1),
UIColor(red: 130/255.0, green: 130/255.0, blue: 130/255.0, alpha: 1),
UIColor(red: 3/255.0, green: 50/255.0, blue: 90/255.0, alpha: 1)]
func regresaColorAleatorio() -> UIColor{
let posicion = Int(arc4random()) % colores.count
return colores(posicion)
}
}
答案 0 :(得分:2)
你犯了一些语法错误。 colores(posicion)
应为colores[posicion]
struct Colores {
let colores = [UIColor(red: 210/255.0, green: 90/255.0, blue: 45/255.0, alpha: 1),
UIColor(red: 40/255.0, green: 170/255.0, blue: 45/255.0, alpha: 1),
UIColor(red: 3/255.0, green: 180/255.0, blue: 90/255.0, alpha: 1),
UIColor(red: 210/255.0, green: 190/255.0, blue: 5/255.0, alpha: 1),
UIColor(red: 120/255.0, green: 120/255.0, blue: 50/255.0, alpha: 1),
UIColor(red: 130/255.0, green: 80/255.0, blue: 90/255.0, alpha: 1),
UIColor(red: 130/255.0, green: 130/255.0, blue: 130/255.0, alpha: 1),
UIColor(red: 3/255.0, green: 50/255.0, blue: 90/255.0, alpha: 1)]
func regresaColorAleatorio() -> UIColor{
let posicion = Int(arc4random()) % colores.count
return colores[posicion]
}
}