Xcode CGVector中的Swift枚举引发错误
XCode 6 B6 尝试创建一个枚举,该枚举可以通过计算CGVector的某些逻辑创建,如下所示:Xcode CGVector中的Swift枚举引发错误,xcode,enums,swift,Xcode,Enums,Swift,XCode 6 B6 尝试创建一个枚举,该枚举可以通过计算CGVector的某些逻辑创建,如下所示: enum Direction:Int, Printable{ case None = 0 case North, South, East, West, NorthEast, NorthWest, SouthEast, SouthWest var description: String{ switch (self){ case .None:
enum Direction:Int, Printable{
case None = 0
case North, South, East, West, NorthEast, NorthWest, SouthEast, SouthWest
var description: String{
switch (self){
case .None:
return "static"
case .North:
return "north"
case .South:
return "south"
case .East:
return "east"
case .West:
return "west"
case .NorthEast:
return "north-east"
case .NorthWest:
return "north-west"
case .SouthEast:
return "south-east"
case .SouthWest:
return "south-west"
}
}
}
extension Direction {
func fromCGVector(vector:CGVector) -> Direction{
var vectorDir = (dx:vector.dx, dy:vector.dy)
switch(vectorDir){
case (0.0, 0.0):
return Direction.None
case let (0.0,y) where y > 0.0:
return Direction.North
case let (0.0,y) where y < 0.0:
return Direction.South
case let (x, 0.0) where x > 0.0:
return Direction.East
case let (x, 0.0) where x < 0.0:
return Direction.West
case let (x, y) where x > 0.0 && y > 0.0:
return Direction.NorthEast
case let (x, y) where x > 0.0 && y < 0.0:
return Direction.SouthEast
case let (x, y) where x < 0.0 && y > 0.0:
return Direction.NorthWest
case let (x, y) where x < 0.0 && y < 0.0:
return Direction.SouthWest
default:
return Direction.None
}
}
}
var test = Direction.fromCGVector(CGVector(dx:1.0, dy:1.0))
枚举方向:Int,可打印{
案例无=0
案例北、南、东、西、东北、西北、东南、西南
变量说明:字符串{
开关(自){
案例:无:
返回“静态”
北区:
返回“北”
案例.南部:
返回“南方”
案例.东部:
返回“东”
案例.西部:
返回“西部”
案例.东北:
返回“东北”
案件.西北:
返回“西北”
案例.东南部:
返回“东南”
案例.西南:
返回“西南”
}
}
}
延伸方向{
func fromCGVector(向量:CGVector)->方向{
var vectorDir=(dx:vector.dx,dy:vector.dy)
开关(矢量目录){
案例(0.0,0.0):
返回方向。无
案例let(0.0,y),其中y>0.0:
返回方向。向北
案例let(0.0,y),其中y<0.0:
返回方向。向南
案例let(x,0.0),其中x>0.0:
返回方向。向东
案例let(x,0.0),其中x<0.0:
返回方向。向西
案例let(x,y),其中x>0.0&&y>0.0:
返回方向:东北
小格(x,y),其中x>0.0&&y<0.0:
返回方向:东南
小格(x,y),其中x<0.0&&y>0.0:
返回方向:西北
小格(x,y),其中x<0.0&&y<0.0:
返回方向:西南
违约:
返回方向。无
}
}
}
var测试=方向。从CGVector(CGVector(dx:1.0,dy:1.0))
但出现以下错误:“CGVector无法转换为方向”
这对我来说没有意义,因为我并没有尝试进行转换,只是调用方向枚举的静态方法。我认为这可能是一个bug,但我想确保我没有遗漏任何明显的内容。我相信您的意思是将该方法声明为
静态
。鉴于枚举不能有任何公共构造函数,因此调用类型上的实例方法而不是它的一个实例是没有意义的
extension Direction {
static func fromCGVector(vector:CGVector) -> Direction?{
///...
var test = Direction.fromCGVector(CGVector(dx:1.0, dy:1.0))
总之,
Direction.None
不是nil
,因此您的fromCGVector
可能会返回一个简单的方向。啊,那个问号是一个意外留下的测试。谢谢你指出这一点,我把它从实际代码中删除了。我知道我忽略了一些简单的东西。非常感谢你!