Giter Site home page Giter Site logo

<== not working for me about arrow HOT 6 CLOSED

freshos avatar freshos commented on May 22, 2024
<== not working for me

from arrow.

Comments (6)

s4cha avatar s4cha commented on May 22, 2024

Hi @Sina-KH ,

I just tried an example in a test project and I can't unfortunately reproduce the issue :(

According to your example I assume your json looks like so :

{
    "customModel": {
        "id" : 1234,
        "title" : "Great!"
    }
}
struct OneModel:ArrowParsable {

    var customModel = CustomModel()

    init() { }
    init(json: JSON) {
        customModel <== json["customModel"]
    }
}
struct CustomModel:ArrowParsable {

    var id = Int()
    var title = String()

    init() { }
    init(json: JSON) {
        id <-- json["id"]
        title <-- json["title"]
    }
}

then I run :

 let json:JSON = jsonForName("OneModel")!
 let m = OneModel(json: json)
 print(m.customModel.id)
 print(m.customModel.title)

which prints out :

1234
Great!

Do you have a compiler error? a runtime crash? or something like that ?

from arrow.

Sina-KH avatar Sina-KH commented on May 22, 2024

Unbelievably It's working now!!! Before this It wasn't parsing! Sorry about that.

But I have another problem bro, How can I parse an array of custom objects with Arrow?

from arrow.

s4cha avatar s4cha commented on May 22, 2024

Hey @Sina-KH
Excuse the late reply.
At the moment there is no Arrowish way to easily parse arrays.
The way you would do it is the manual way which looks like so :

for a property

var phoneNumbers:[PhoneNumber] = [PhoneNumber]()

and the following JSON

{
    "id": 15678,
    "created_at": "2013-06-07T16:38:40+02:00",
    "name": "Francky",
    "stats": {
        "numberOfFriends": 163,
        "numberOfFans": 10987
    },
    "phoneNumbers": [{
        "label": "house",
        "number": "9809876545"
    }, {
        "label": "cell",
        "number": "0908070656"
    }, {
        "label": "work",
        "number": "0916570656"
    }]
}

The parsing would be the following :

        phoneNumbers = [PhoneNumber]()
        if let pns = json["phoneNumbers"] as? [AnyObject] {
            for pn in pns {
                phoneNumbers.append(PhoneNumber(json: pn))
            }
        }

of course with the following mapping :

struct PhoneNumber {
    var label:String = ""
    var number:String = ""
}


extension PhoneNumber:ArrowParsable {
    init(json: JSON) {
        label <-- json["label"]
        number <-- json["number"]
    }
}

Hope it helps :)

from arrow.

s4cha avatar s4cha commented on May 22, 2024

Actually here is a snippet that would enable parsing arrays with <== syntax :)

public func <== <T:ArrowParsable>(inout left:[T], right: AnyObject?) {
    left = [T]()
    if let pns = right as? [AnyObject] {
        for pn in pns {
            left.append(T(json:pn))
        }
    }
}

from arrow.

s4cha avatar s4cha commented on May 22, 2024

Array parsing has been added in the latest version :)

from arrow.

Sina-KH avatar Sina-KH commented on May 22, 2024

Good to hear that.
Finally I can use arrow in my projects easier than ever!
Thanks, Good Job!

from arrow.

Related Issues (19)

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.