2

我们如何在 Swift 中做类似前向声明的事情?

我在类之前声明了一个协议,但是协议需要使用在类之前声明的枚举(recordType):

protocol iCloudDelegate {
    func errorUpdating(error: NSError)
    func iCloudUpdated()
    func iCloudFetched(recordType: recordType)
}

class iCloud {

    enum recordType : String {
        case Payment = "Payment"
        case Subtype = "Subtype"
        case Types = "Type"
        case Entry = "Entry"
        case Repeat = "Repeat"
    }
}

现在 Swift 编译器抱怨Use of undeclared type 'recordType'错误。在 Objective-C 中,我们会放置某种前向声明,那么 Swift 呢?

作为一个附注问题,您看到上面我必须声明 case Types,而不是 Type,因为“Type”显然是枚举的保留 case-word。有什么办法可以克服这个吗?(当然,除了像我一样更改名称)

4

1 回答 1

4

要访问在另一个类型(嵌套类型)中声明的类型,请将周围类型的名称放在嵌套类型之前:

func iCloudFetched(recordType: iCloud.recordType)

For more information about Nested Types in Swift: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/NestedTypes.html

于 2015-03-04T09:40:38.320 回答