Quick mistake. An undeclared String type is used

if we insert the line "case let dictionary as [String: AnyObject]:" inside the struct method everything works fine. But if used inside nested enums, we get the 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

      

Can anyone explain why I am having an error with a string type?

+3


source to share


3 answers


It seems to enum Type

contain case String

and it hides String

what you want. I tried the code in Playground and after change there String

is no more error.

EDIT How about reading a SwiftyJSON project (only one file)

https://github.com/SwiftyJSON/SwiftyJSON/blob/master/Source/SwiftyJSON.swift

I am very similar to work (JSON Handling)



It also contains code that 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 think you can use this project)

+9


source


As said in another answer, String

internally enum Type

refers to the enum value. The same problem would arise when using

let a : Array<Int> = []
let b : Bool = false

      

inside methods enum Type

. Renaming the enum values ​​is probably the best solution.



But for the sake of completeness: you can fix the problem by adding the module name "Swift" explicitly to reference the String

type:

case let dictionary as [Swift.String : AnyObject]:

      

+1


source


If you want to fix this without renaming the enum case, you can change the type of the argument to Swift.String

, i.e .:

case let dictionary as [Swift.String : AnyObject]:

This should work (I had a similar problem and this solved it).

0


source







All Articles