Swift 错误。使用未声明的类型 String
Swift error. Used undeclared type String
如果我们在结构的方法中插入行 "case let dictionary as [String : AnyObject]:" 一切正常。但是如果在嵌套枚举中使用,我们会得到错误 "Used undefined type String"
public struct JSON {
public enum Type : Int {
case Number
case String
case Bool
case Array
case Dictionary
case Null
case Unknown
public static func evaluate(object: AnyObject) -> Type {
switch object {
case let dictionary as [String : AnyObject]: // this lines supply error. Use of undefined type String
return .Dictionary
default:
return .Unknown
}
}
} // enum Type
有人能解释一下为什么我的字符串类型有错误吗?
似乎 enum Type
包含 case String
,它隐藏了您想要的 String
。我在 Playground 中尝试了代码,将 String
更改为另一个名称后没有更多错误。
编辑
阅读项目如何SwiftyJSON(仅单个文件)
https://github.com/SwiftyJSON/SwiftyJSON/blob/master/Source/SwiftyJSON.swift
我做的工作非常相似。(JSON处理)
它还包含如下代码:
public enum Type :Int {
case Number
case String
case Bool
case Array
case Dictionary
case Null
case Unknown
}
我认为这个项目对你很有帮助。
(我猜你最终可能会使用这个项目)
正如在另一个答案中已经说过的,String
里面 enum Type
引用枚举值。
也会出现同样的问题
let a : Array<Int> = []
let b : Bool = false
enum Type
的内部方法。重命名枚举值可能是最好的解决方案。
但为了完整起见:您可以解决问题
通过在 "Swift" 模块名称前显式引用
String
类型:
case let dictionary as [Swift.String : AnyObject]:
如果你想在不重命名枚举大小写的情况下解决这个问题,你可以将参数类型更改为 Swift.String
,即:
case let dictionary as [Swift.String : AnyObject]:
这应该有效(我遇到了类似的问题,这解决了它)。
如果我们在结构的方法中插入行 "case let dictionary as [String : AnyObject]:" 一切正常。但是如果在嵌套枚举中使用,我们会得到错误 "Used undefined type String"
public struct JSON {
public enum Type : Int {
case Number
case String
case Bool
case Array
case Dictionary
case Null
case Unknown
public static func evaluate(object: AnyObject) -> Type {
switch object {
case let dictionary as [String : AnyObject]: // this lines supply error. Use of undefined type String
return .Dictionary
default:
return .Unknown
}
}
} // enum Type
有人能解释一下为什么我的字符串类型有错误吗?
似乎 enum Type
包含 case String
,它隐藏了您想要的 String
。我在 Playground 中尝试了代码,将 String
更改为另一个名称后没有更多错误。
编辑 阅读项目如何SwiftyJSON(仅单个文件)
https://github.com/SwiftyJSON/SwiftyJSON/blob/master/Source/SwiftyJSON.swift
我做的工作非常相似。(JSON处理)
它还包含如下代码:
public enum Type :Int {
case Number
case String
case Bool
case Array
case Dictionary
case Null
case Unknown
}
我认为这个项目对你很有帮助。 (我猜你最终可能会使用这个项目)
正如在另一个答案中已经说过的,String
里面 enum Type
引用枚举值。
let a : Array<Int> = []
let b : Bool = false
enum Type
的内部方法。重命名枚举值可能是最好的解决方案。
但为了完整起见:您可以解决问题
通过在 "Swift" 模块名称前显式引用
String
类型:
case let dictionary as [Swift.String : AnyObject]:
如果你想在不重命名枚举大小写的情况下解决这个问题,你可以将参数类型更改为 Swift.String
,即:
case let dictionary as [Swift.String : AnyObject]:
这应该有效(我遇到了类似的问题,这解决了它)。