let NumberOfColor: UInt32 = 6
enum BlockColor:Int, CustomStringConvertible{
case Blue = 0, Orange, Purple, Red, Teal, Yellow
var spriteName: String{
switch self {
case .Blue:
return "blue"
case .Orange:
return "orange"
case .Purple:
return "purple"
case .Red:
return "red"
case .Yellow:
return "yellow"
}
}
static func random() -> BlockColor{
return BlockColor(rawValue:Int(arc4random_uniform(NumberOfColor)))!
}
}
错误是"枚举BlockColor:Int,CustomStringConvertible {...}"
键入' BlockColor'不符合协议' CustomStringConvertible'
我尝试通过swift制作游戏,但我的代码有问题 谁能告诉我为什么会发生这个错误? (我的英语不好,这是我的第一个问题,谢谢大家
这个问题已经完成,谢谢。 但有同样的问题
class Block: Hashable, CustomReflectable{
let color:BlockColor
var column: Int
var row: Int
var sprite: SKSpriteNode?
var spriteName: String{
return color.spriteName
}
var hashValue: Int{
return self.column ^ self.row
}
init(column:Int, row:Int, color:BlockColor){
self.column = column
self.row = row
self.color = color
}
var description: String{
return "\(color): [\(column), \(row)]"
}
} func ==(lhs:Block,rhs:Block) - >布尔{ return lhs.column == rhs.column&& lhs.row == rhs.row&& lhs.color.rawValue == rhs.color.rawValue }
答案 0 :(得分:1)
将您的代码粘贴到游乐场并阅读错误消息
Swift.CustomStringConvertible:15:16:注意:协议要求属性'description'的类型为'String'
或者CustomStringConvertible
上的⌘-点击以找出所需的方法。
错误消息表示您必须在description
enum
var description : String {
return "\(self.rawValue)"
}
PS:您也可以简单地将spriteName
替换为description