Coder Social home page Coder Social logo

Comments (4)

gfontenot avatar gfontenot commented on June 4, 2024
  • You should be using the <| operator for retrieving values from JSON:
let id: Int = j <| "id"
let text: String = j <| "text"
  • You can get values of the type [String: JSONValue] with a simple custom parser:
func getDictionary(j: JSONValue) -> [String: JSONValue]? {
  switch j {
  case .let JSONObject(o): return o
  default: return .None
  }
}

That being said, we already have a built-in syntax for traversing dictionaries for nested keys:

let nested: String = j <| ["path", "to", "nested", "key"]

I highly suggest reading as much of the README as possible. I know it's long, but there's a ton of info in there.

I'm going to close this issue, but please feel free to keep asking questions if any of this still doesn't make sense.

from argo.

tonyd256 avatar tonyd256 commented on June 4, 2024

As stated above, you should use the built in conventions for retrieving the data from the JSON. However, number 1 would be possible (although not recommended). You might want to read up on Optionals and their uses ... here is a little explanation to what you were seeing:

For that particular line: let id: Int = j["id"]?.value(), the j["id"] will return a JSONValue? which you can then optionally unwrap and call value(). value() returns the type you are requesting wrapped in an optional. In this case you are requesting an Int so it will return Int?. You cannot assign an optional value to a non optional variable so it fails with that error message you are seeing. The solution would be to explicitly unwrap (always a bad idea) the optional to get the Int. However, throwing an ! at the end of this statement doesn't help because the j["id"]? could still fail and return .None. To get directly at the value wrapped by the optional, the statement would look like this: let id: Int = j["id"]!.value()! ... again you shouldn't do this. Instead, use the value as an optional, let id: Int? = j["id"]?.value() or unwrap it using if-let syntax like so:

if let id = j["id"]?.value() as Int? {
  println(id)
}

That being said, still try to use Argo as intended as most of this value stuff will be deprecated in future versions.

from argo.

king7532 avatar king7532 commented on June 4, 2024

Thank you @tonyd256 and @gfontenot for the explanation, I now have a much better understanding. One last question, can you explain this error message for:

if let text = j <| "text" {
            println(text)
        }
error: cannot invoke '<|' with an argument list of type '(JSONValue, StringLiteralConvertible)'
        if let text = j <| "text" {
                      ~~^~~~~~~~~

When the correct code is:

if let text = j <| "text" as String? {
    println(text)
}

from argo.

tonyd256 avatar tonyd256 commented on June 4, 2024

This is because the <| operator is relying on type inference meaning that you need to tell it what type you want through a cast like in your second example. The error message is kind of misleading but basically Swift doesn't know what type you want so it's confused. By casting the variable or the result of the expression, you are telling the operator which type to try and return.

from argo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.