Comments (6)
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.
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.
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.
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.
Array parsing has been added in the latest version :)
from arrow.
Good to hear that.
Finally I can use arrow in my projects easier than ever!
Thanks, Good Job!
from arrow.
Related Issues (19)
- Doesn't compile with snapshot 4-12 HOT 5
- Value of type 'JSON' has no member 'collection' HOT 2
- Array of enums HOT 5
- Is the swift 3 branch stable? HOT 6
- No inheritance on Structs HOT 2
- Release 3.0.3 not available on master HOT 1
- Does this library allow to serialize a JSON string to an object? HOT 4
- Get notified if a key is missing HOT 3
- please make dateFormatter in Arrow.swift public HOT 4
- Swift 4 HOT 3
- Codable HOT 3
- Cannot cast json to other type HOT 3
- Support optional data for custom models
- How do I instantiate a model directly from a string containing json? HOT 4
- How do you use Arrow with Moya+RxSwift HOT 5
- netsted values problem
- Doesn't compile on Linux OS HOT 1
- Privacy Manifest HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arrow.