斯威夫特:期待','失败?

时间:2014-10-13 22:35:57

标签: ios xcode macos swift

我对编程很陌生,我一步一步地按照指南,一切正常,但此时我被卡住了。我已经检查了所有内容,但它说每次我尝试修复它时都会出现故障并构建它会出现故障,例如Expected expressions in list of expressionsExpected ',' separator'()' is not convertible to 'Orientation'

import Foundation
import SpriteKit

let NumOrientations: UInt32 = 4

enum Orientation: Int, Printable {
    case Zero = 0, Ninety, OneEighty, TwoSeventy

    var description: String {
        switch self {
        case .Zero:
            return "0"
        case .Ninety:
            return "90"
        case .OneEighty:
            return "180"
        case .TwoSeventy:
            return "270"
        }
    }

    static func random() -> Orientation {
        return Orientation.fromRaw(<#Int#>(arc4random_uniform(NumOrientations)))
    }

// #1
    static func rotate(orientation:Orientation, clockwise: Bool) -> Orientation {
        var rotated = orientation.toRaw() + (clockwise ? 1 : -1)
        if rotated > Orientation.TwoSeventy.toRaw() {
            rotated = Orientation.Zero.toRaw()
        } else if rotated < 0 {
            rotated = Orientation.TwoSeventy.toRaw()
        }
        return Orientation.fromRaw(rotated)!
    }
}

有任何帮助吗?

1 个答案:

答案 0 :(得分:1)

我不知道你在哪里得到&lt; #Int#&gt;语法来自,但这对我有用:

static func random() -> Orientation! {  
    return Orientation.fromRaw(Int(arc4random_uniform(NumOrientations)))
}

请注意,返回类型应为Orientation!,而不是Orientation,因为它不是可选的。

同样适用于您的rotate功能

static func rotate(orientation:Orientation, clockwise: Bool) -> Orientation! {
    var rotated = orientation.toRaw() + (clockwise ? 1 : -1)
    if rotated > Orientation.TwoSeventy.toRaw() {
        rotated = Orientation.Zero.toRaw()
    } else if rotated < 0 {
        rotated = Orientation.TwoSeventy.toRaw()
    }
    return Orientation.fromRaw(rotated)
}

请注意,您可以将rotate函数设置为实例方法而不是静态,这是更好的面向对象设计

func rotate(#clockwise:Bool) -> Orientation! {
    var rotated = self.toRaw() + (clockwise ? 1 : -1)
    if rotated > Orientation.TwoSeventy.toRaw() {
        rotated = Orientation.Zero.toRaw()
    } else if rotated < 0 {
        rotated = Orientation.TwoSeventy.toRaw()
    }
    return Orientation.fromRaw(rotated)
}