1

I am just tired with this package... is this correct? So if a JSON type returns UNKNOWN then later i do check for a key it returns TRUE?

let json = JSON(jsonFromServer)
print(json) // prints "unknown"
print(json["key"].exists()) // returns true?!

What does UNKNOWN mean ? I've tested the code with good cases where it did work. Now if the server returns no JSON my code is buggy. What do I do wrong or how can I check if JSON() returns unknown? It's so poorly documented. Do you recommend another package or should I use the default one if this has no solution?

robertsan
  • 1,591
  • 3
  • 14
  • 26
  • What's `jsonFromServer`, is it `Data` or an object? If I remember correctly for data you need `JSON(data: jsonFromServer)` otherwise the initializer fails silently. I've stopped using SwiftyJSON, it's become too unstable in the last versions. But `exists` definitely [works](http://stackoverflow.com/a/37169327/2227743)... or should I say "worked" now? :/ – Eric Aya Nov 04 '16 at 16:43
  • I would never directly convert serverJSON into SwiftyJSON JSON, but maybe it is just a lack of faith in my backend dev. – Sethmr Nov 04 '16 at 18:01

1 Answers1

3

From SwiftyJson's code, I can say that its any of the type that is not supported by swifty-json:

public enum Type :Int{
    case Number
    case String
    case Bool
    case Array
    case Dictionary
    case Null
    case Unknown
}

To check for error, you will have error set in json:

default:
    _type = .Unknown
    _error = NSError(domain: ErrorDomain, code: ErrorUnsupportedType, userInfo: [NSLocalizedDescriptionKey: "It is a unsupported type"])
}

Personally, I find it self-documented, still refer to this link for detailed documentation/example usage.

Sunil Chauhan
  • 2,074
  • 1
  • 15
  • 33
  • For me it was the NSUUID which was causing SwiftyJSON to fail and took me many hour to figure this out. I had to convert NSUUID to String to fix this error. – zeeshan Jan 04 '19 at 02:19