Reputation: 117
if we insert line "case let dictionary as [String : AnyObject]:" inside struct's method everything works fine. But if use inside nested enums we get error "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
Could some one explain why I have the error with String type?
Upvotes: 3
Views: 5449
Reputation: 67
If you want to fix this without renaming the enum case, you can change the argument type to Swift.String
, i.e.:
case let dictionary as [Swift.String : AnyObject]:
That should work (I had a similar problem and this solved it).
Upvotes: 0
Reputation: 1933
It seems enum Type
contains case String
, and it hides the String
what you want. I tried the code in Playground and there is no more error after change String
to another name.
EDIT How about reading the project SwiftyJSON (only single file)
https://github.com/SwiftyJSON/SwiftyJSON/blob/master/Source/SwiftyJSON.swift
I does very similar job.(JSON Handling)
It also contains the code looks like this:
public enum Type :Int {
case Number
case String
case Bool
case Array
case Dictionary
case Null
case Unknown
}
I think this project will be very helpful for you. (and I guess you may end up using this project)
Upvotes: 9
Reputation: 540075
As already said in the other answer, String
inside enum Type
refers to the enumeration value. The same problem would occur with
let a : Array<Int> = []
let b : Bool = false
inside methods of enum Type
. Renaming the enumeration values is probably the best solution.
But for the sake of completeness: You can solve the problem
by prepending the "Swift" module name explicitly to refer to the
String
type:
case let dictionary as [Swift.String : AnyObject]:
Upvotes: 1