我们如何在 Swift 中执行诸如前向声明之类的操作?
我在类之前声明了一个协议(protocol),但协议(protocol)需要使用在类之前声明的枚举(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 - 在 Swift 中使用类本身协议(protocol)中声明的枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28850223/