I know with SwiftyJSON you can convert the objects from JSON
to Swift
.
Does SwiftyJSON allows you to go back? i.e. take NSManagedObject
s with relationships and converting them it into JSON?
Example Please.
You can't do that, that's not what the SwiftyJSON is made for. SwiftyJSON is just using the features of Swift for better parsing of JSON compared to objective-c, it wouldn't bring any value for serialization to JSON.
For your purpose, you have to create dictionary/array from your NSManagedObject
object. Then use just Alamofire
with JSON serializer like this:
let parameters = event.toJSON() // create Dictionary from NSManagedObject
Alamofire.request(.POST, "https://httpbin.org/post", parameters: parameters, encoding: .JSON)
The serialization to JSON dictionary – if you have two subclasses of NSManagedObject
– Event
and Activity
where Event
has one-to-many relation to Activity
, I would go like this:
extension Event {
func toJSON() -> Dictionary<String, AnyObject> {
return [
"id": self.id,
"name": self.name,
"startDate": self.startDate.GMTFormatString,
"endDate": self.endDate.GMTFormatString,
"activities": self.activities.map({ $0.toJSON() })
]
}
}
extension Activity {
func toJSON() -> Dictionary<String, AnyObject> {
return [
"id": self.id,
"name": self.name
]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With