Ios 类型()不符合未定义的协议
错误是“enum BlockColor:Int,CustomStringConvertible{…}” 类型“BlockColor”不符合协议“CustomStringConverable” 我试着用斯威夫特制作这个游戏,但代码有问题 有人能告诉我为什么会发生这个错误吗? (我的英语不好,这是我的第一个问题,谢谢大家 这个问题已经解决了,谢谢。 但我有同样的问题Ios 类型()不符合未定义的协议,ios,swift,Ios,Swift,错误是“enum BlockColor:Int,CustomStringConvertible{…}” 类型“BlockColor”不符合协议“CustomStringConverable” 我试着用斯威夫特制作这个游戏,但代码有问题 有人能告诉我为什么会发生这个错误吗? (我的英语不好,这是我的第一个问题,谢谢大家 这个问题已经解决了,谢谢。 但我有同样的问题 let NumberOfColor: UInt32 = 6 enum BlockColor:Int, CustomStringCon
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)))!
}
}
}
func==(左:块,右:块)->Bool{
返回lhs.column==rhs.column&&lhs.row==rhs.row&&lhs.color.rawValue==rhs.color.rawValue
}在操场上粘贴代码并阅读错误消息 Swift.CustomStringConvertible:15:16:注意:协议要求属性“description”的类型为“String” 或者⌘-单击
CustomStringConvertible
了解所需的方法
错误消息意味着您必须在enum
中实现属性description
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)]"
}
PS:你也可以简单地用
描述替换spriteName
请发布代码,而不是代码的图像。我不知道如何发布代码,有人可以教我?:(@xiebochen这比发布图像容易:复制和粘贴(⌘C-⌘五) 将代码粘贴到问题中。它需要缩进4个空格才能识别为代码,并确保代码上方和下方有一个空行。如果需要添加缩进,请在SO编辑器中拖动并选择代码,然后按{}
工具栏中的按钮。非常感谢,我已经完成了我的帖子,但我不知道是什么错误……我尝试添加此描述,但Xcode告诉添加“默认值”在switch中,你能告诉我为什么吗?你忘了在switch语句中添加caseTeal
。非常感谢!!!它工作正常,但是…还有其他问题,我创建了类,还使用了“CustomReflectable”并实现了“description”,但它告诉我有与前面相同的错误CustomReflectable
与customStringConverable
不同,需要不同的方法/属性。如上所述⌘-单击CustomReflectable
查找。@vadian非常感谢您,我将使用⌘-单击以解决问题,谢谢!!
var description : String {
return "\(self.rawValue)"
}