Coder Social home page Coder Social logo

Comments (5)

pcantrell avatar pcantrell commented on May 22, 2024

I assume that response.json is the typed content accessor from the example project that returns SwiftyJSON, right?

What's going on here is that at this point:

var res = response.json
//example manipulation
res["cbs"][0]["input"][0]["value"].string = "00000"

res is a SwiftyJSON.JSON object, but NSJSONConvertible wants a dictionary or array. You have two options:

  1. Convert the JSON back to an NSDictionary to pass it to Siesta:

    api.login.request(.POST, json: res.dictionary)
    
  2. If you're doing that in many places, add a Siesta request flavor that accepts JSON objects:

    public func request(method: RequestMethod, jsonDict: JSON) -> Request
        { return request(method, json: json.dictionary) }
    

(Warning: untested code there!)

from siesta.

lifeisfoo avatar lifeisfoo commented on May 22, 2024

Yes, res is a SwiftyJSON.JSON.

I've already tried with .dictionary but seems that it generates a non valid type for the method signature:

Argument type '[String : JSON]?' does not conform to expected type 'NSJSONConvertible'

from siesta.

pcantrell avatar pcantrell commented on May 22, 2024

Ah, indeed. Here is some code that I’ve verified actually compiles:

extension Resource
    {
    public func request(method: RequestMethod, jsonDict: JSON) -> Request
        { return request(method, json: json.object as! NSJSONConvertible) }
    }

See if that does the trick.

(Hmm. I should expose FailedRequest in the public API so you can replace that as! with an as?.)

from siesta.

lifeisfoo avatar lifeisfoo commented on May 22, 2024

Yes, this is right, but there is a typo in the call: the parameter is called jsonDict, not json :).

extension Resource
    {
    public func request(method: RequestMethod, jsonDict: JSON) -> Request
        { return request(method, json: jsonDict.object as! NSJSONConvertible) }
    }

Thank you for the help.

Do you think that could be useful to add this example in a "Cookbook" page inside Siesta docs?

from siesta.

pcantrell avatar pcantrell commented on May 22, 2024

the parameter is called jsonDict, not json

Ah, right.

I mean to write up a cookbook for 1.0, but am trying to get API details sorted out first.

I may also add a standard SwiftyJSON extension, now that there's an Extensions folder for such things.

from siesta.

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.