在 Swift 中的 class 本身的协议中使用在 class 中声明的枚举?
Using enum declared in class in the protocol of the class itself in Swift?
我们如何在 Swift 中做前向声明之类的事情?
我有一个在 class 之前声明的协议,但是协议需要使用在 class 之前声明的枚举 (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 编译器抱怨 使用未声明的类型 'recordType' 错误。在 objective-C 中,我们将放置某种前向声明,那么 Swift 呢?
作为旁注问题,您看到上面我必须声明 case Types,而不是 Type,因为 "Type" 显然是枚举的保留 case-word。有什么办法可以克服这个问题吗? (除了像我一样更改名称当然)
要访问在另一个类型(嵌套类型)中声明的类型,请将周围类型的名称放在嵌套类型之前:
func iCloudFetched(recordType: iCloud.recordType)
有关 Swift 中嵌套类型的更多信息:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/NestedTypes.html
我们如何在 Swift 中做前向声明之类的事情?
我有一个在 class 之前声明的协议,但是协议需要使用在 class 之前声明的枚举 (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 编译器抱怨 使用未声明的类型 'recordType' 错误。在 objective-C 中,我们将放置某种前向声明,那么 Swift 呢?
作为旁注问题,您看到上面我必须声明 case Types,而不是 Type,因为 "Type" 显然是枚举的保留 case-word。有什么办法可以克服这个问题吗? (除了像我一样更改名称当然)
要访问在另一个类型(嵌套类型)中声明的类型,请将周围类型的名称放在嵌套类型之前:
func iCloudFetched(recordType: iCloud.recordType)
有关 Swift 中嵌套类型的更多信息:https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/NestedTypes.html