Mapping local JSON file to Realm object using Object Mapper - json

I have local JSON file and i am trying to map that json to realm object using ObjectMapper_Realm library.
var totalLessonArray = [Lesson]()
if let path = Bundle.main.path(forResource: "data", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let jsonResult = try JSONSerialization.jsonObject(with: data, options: [])
totalLessonArray = Mapper<Lesson>().mapArray(JSONArray: jsonResult as! [[String : Any]])
} catch {
// handle error
}
}
When i print totalLessonArray i get following error in my 4 level deep list
//...
quizScore = 0;
quizQuestions = List<Question> <0x6000022e4d80> (
[0] Question {
question = What are the synonym of sympathy? Drag and drop in the box below.;
answer = <Maximum depth exceeded>;
}
);
//...
This is the model file
class Lesson : Object, Mappable{
#objc dynamic var lessonNumber : Int = 0
#objc dynamic var totalChapter : Int = 0
#objc dynamic var completedChapter : Int = 0
#objc dynamic var quizTaken : Bool = false
#objc dynamic var isFinished : Bool = false
#objc dynamic var isCurrentlyWatchingLesson : Bool = false
#objc dynamic var currentlyWatchingChapter : Int = 0
#objc dynamic var lessonTitle : String = ""
#objc dynamic var quizScore : Int = 0
var chapter = List<Chapter>()
var quizQuestions = List<Question>()
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
lessonNumber <- map["lessonNumber"]
totalChapter <- map["totalChapter"]
completedChapter <- map["completedChapter"]
quizTaken <- map["quizTaken"]
isFinished <- map["isFinished"]
isCurrentlyWatchingLesson <- map["isCurrentlyWatchingLesson"]
currentlyWatchingChapter <- map["currentlyWatchingChapter"]
lessonTitle <- map["lessonTitle"]
quizScore <- map["quizScore"]
chapter <- (map["chapter"], ListTransform<Chapter>())
quizQuestions <- (map["quizQuestions"], ListTransform<Question>())
}
}
I have json which is 4-5 level down deep. Am i doing something wrong?

Related

How to return object from nested json

I have nested JSON and I'm trying to return an object and update UI. However, I can access properties only from Result array, but can't reach Step from AnalyzedInstactions.
Here are classes that represent data in the JSON. Generated it with https://app.quicktype.io
import UIKit
class ResultArray: Codable {
var results = [Result]()
}
class Result: Codable, CustomStringConvertible {
var title: String = ""
var image = ""
var readyInMinutes: Int? = 0
var servings: Int? = 0
var cuisines = [String]()
var dishTypes = [String]()
var diets = [String]()
var occasions = [String]()
var analyzedInstructions = [AnalyzedInstruction]()
var description: String {
return "\nResults - Name: \(title), Summary: \(String(describing: readyInMinutes ?? nil)), \(String(describing: servings ?? nil)) "
}
}
// MARK: - Steps
class AnalyzedInstruction: Codable {
var name: String? = ""
var steps = [Step]()
}
class Step: Codable {
var number: Int = 0
var step: String = ""
}
Here is my parse method
private func parse(data: Data) -> [Result] {
do {
let decoder = JSONDecoder()
let result = try decoder.decode(ResultArray.self, from: data)
return result.results
} catch {
print("JSON Error: \(error)")
return []
}
}
try this simple code to access your steps from AnalyzedInstructions:
let results: [Result] = parse(data: theData)
if let firstResult = results.first {
if let firstAnaInst = firstResult.analyzedInstructions.first {
for step in firstAnaInst.steps {
print("--> step.step: \(step.step) step.number: \(step.number)")
}
} else { print(" NO analyzedInstructions") }
} else { print(" NO results") }
if you want all steps:
for result in results {
for anaInst in result.analyzedInstructions {
for step in anaInst.steps {
print("--> step.step: \(step.step) step.number: \(step.number)")
}
}
}
PS: if appropriate, you may consider using struct instead of class for your json models.

Parsing JSON & work with ObjectMapper,SwiftRealm,ObjectMapperAdditions,ObjectMapperAdditionsRealm & Alamofire

regards, I am trying to serialize a json from the placeHolder page, the users one (https://jsonplaceholder.typicode.com/ users), I am trying to use the following libraries together:
-> Alamofire
-> RealmSwift
-> ObjectMapper
-> ObjectMapperAdditions
-> ObjectMapper_Realm
Here is the model that I am defining, but the problem is that SwiftRealm does not support the ArrayDictionary data type, as you try changing the type in the COMPANY & ADDRESS attributes, which are of the array type, try to put it as List , [Address1]?, Address1, List , etc. but it does not work, it always marks the same error "The operation could not be completed, ObjectMapper could not serialize the response.", so it can be received as an object but it can not be saved in RealmSwift or failing that What to do, but you will also receive as JSON or ARRAY and it is also not possible to serialize it, have you implemented something like that?
import Foundation
import RealmSwift
import ObjectMapper
import ObjectMapperAdditions
import ObjectMapper_Realm
class PlaceHolderClass: Object, Mappable {
#objc dynamic var id = 0
#objc dynamic var name = ""
#objc dynamic var username = ""
#objc dynamic var email = ""
var address: Address1?
#objc dynamic var phone = ""
#objc dynamic var website = ""
var company: Company?
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
id <- map["id"]
name <- map["name"]
username <- map["username"]
email <- map["email"]
address <- map["address"]
phone <- map["phone"]
website <- map["website"]
company <- map["company"]
}
}
class Address1: Object, Mappable {
var street = List<String>()
var suite = List<String>()
var city = List<String>()
var zipcode = List<String>()
var geo: Geo?
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
street <- map["street"]
suite <- map["suite"]
city <- map["city"]
zipcode <- map["zipcode"]
geo <- map["geo"]
}
}
class Geo: Object, Mappable {
var lat = List<String>()
var lng = List<String>()
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
lat <- map["lat"]
lng <- map["lng"]
}
}
class Company: Object, Mappable {
var name = List<String>()
var catchPhrase = List<String>()
var bs = List<String>()
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
name <- map["name"]
catchPhrase <- map["catchPhrase"]
bs <- map["bs"]
}
}
My request Code:
func requestPlaceHolder(){
let URL = "https://jsonplaceholder.typicode.com/users"
Alamofire.request(URL).responseObject{ (response: DataResponse<PlaceHolderClass>) in
switch response.result {
case .success(let objects):
let realm = try! Realm()
let myPlaceHolderResponse = PlaceHolderClass(value: response.result.value)
try! realm.write {
realm.add(myPlaceHolderResponse, update: true)
print("se agrego correctamente")
}
print(Realm.Configuration.defaultConfiguration.fileURL)
case .failure(let error):
print("Ocurrio el siguiente error \(error.localizedDescription)")
}
print(Realm.Configuration.defaultConfiguration.fileURL)
}
}

Convert json response to Realm object

hi I am trying to convert json response from a web service to realm object and insert it into realm database using object mapper but returns objects with null values
Alamofire.request(url).responseJSON { response in
let products = Mapper<ProRealm>().map(JSONObject:response.result.value)
print("products",products) // object with null values
}
My ProRealm class is
class ProRealm: Object, Mappable {
dynamic var additives_count: String?
var rating: String?
var updated: Bool = false;
var name: String?
var barcode: String?
var product_key: String?
var hazard_count: String?
var state: String?
var no_of_users_rated: String?
var thumbnail: String?
var overall_rating : String?
var is_food_or_beverage : Bool = false
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
additives_count <- map["additives_count"]
rating <- map["rating"]
updated <- map["updated"]
name <- map["name"]
barcode <- map["barcode"]
product_key <- map["product_key"]
hazard_count <- map["hazard_count"]
state <- map["state"]
no_of_users_rated <- map["no_of_users_rated"]
thumbnail <- map["thumbnail"]
overall_rating <- map["overall_rating"]
is_food_or_beverage <- map["is_food_or_beverage"]
}
}
In Json response, the products are inside the products key. please advice how to convert it into realm object
["kind": aWareInternalAPI#productsItem, "products":(
{
"additives_count" = 3;
barcode = 12345;
"hazard_count" = 0;
"is_food_or_beverage" = 1;
name = "Water Bottle";
"no_of_users_rated" = 1;
"overall_rating" = "0.0";
"product_key" = "ahdzfmF3YXJlLWJhY2tlbmQtc3RhZ2luZ3IlCxIEVXNlchiAgICgvoOTCQwLEgdQcm9kdWN0GICAgIDE_JAKDA";
rating = 0;
state = "OCR_PROCESSING";
thumbnail = "http://lh3.googleusercontent.com/dMxwgSQB02osZJJex4S57iupaMT9tjDYZaD7mweJUjYmI1KNEcZZe1syBwrRs1GbYdZNrRUtQwRYUwXiAEscGNYH-J9f3gJOXYO1rQ=s150";
updated = 1;
},
{
"additives_count" = 0;
barcode = 53647825898248485;
"hazard_count" = 0;
"is_food_or_beverage" = 1;
"no_of_users_rated" = 0;
"overall_rating" = "0.0";
"product_key" = ahdzfmF3YXJlLWJhY2tlbmQtc3RhZ2luZ3IlCxIEVXNlchiAgICgvoOTCQwLEgdQcm9kdWN0GICAgICumYAJDA;
rating = "";
state = "OCR_PROCESSING";
thumbnail = "http://lh3.googleusercontent.com/0D55ZXkG8Ua5ULDK69Po-IHeDPIfXZHOi7LlLURoc1qZzmNst57xUMQSPzWTW5miSDglc5wKDA4QlvLvnD6aMOqIHcwlj_HY-Hs=s150";
updated = 1;
})]
Please advice
All your properties except updated and is_food_or_beverage are nilable, so there is a possibility that they might be nil. If you know that they should have a value and you´re still getting nil make sure to check the keys in your mapping function.
Update:
I´m not 100% sure of how this works with the syntax with Mappable, but you need to do it something like this:
class Products: Object, Mappable {
let products: [Product]?
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
products <- map["products"]
}
}
class Product: Object, Mappable {
dynamic var additives_count: String?
var rating: String?
var updated: Bool = false;
var name: String?
var barcode: String?
var product_key: String?
var hazard_count: String?
var state: String?
var no_of_users_rated: String?
var thumbnail: String?
var overall_rating : String?
var is_food_or_beverage : Bool = false
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
additives_count <- map["additives_count"]
rating <- map["rating"]
updated <- map["updated"]
name <- map["name"]
barcode <- map["barcode"]
product_key <- map["product_key"]
hazard_count <- map["hazard_count"]
state <- map["state"]
no_of_users_rated <- map["no_of_users_rated"]
thumbnail <- map["thumbnail"]
overall_rating <- map["overall_rating"]
is_food_or_beverage <- map["is_food_or_beverage"]
}
}
So create two different classes one that holds an array of products and the other is a Product.

tableview store loses reference on ViewDidLoad nested optional unwrapping

I am trying to parse some weather data, and I have ran into an issue. I created a default value in an array that gives the user a dummy location and value in a table view. This value is unwrapped using the same code below that I am trying to use for real data. It works fine for the dummy value and can find the locationStore easily, but when I am loading from the view that has the parsed data, the locationStore is showing as nil in the debugger. I am not sure how to solve this, so any help would be greatly appreciated.
class LocationWeatherViewController: UITableViewController{
var locationStore: LocationStore!
var imageStore: ImageStore!
var newLocation: Location!
var locationCreated : NSMutableArray = NSMutableArray( array: ["Test ", 0, 0.0, 0.0 , 0.0])
override func viewDidLoad() {
super.viewDidLoad()
print(locationCreated.count)
if let locationName = locationCreated[0] as? String {
print(locationName)
if let currentTemp = locationCreated[2] as? Double{
print(currentTemp)
if let zip32 = (locationCreated[1] as? Int){
let zip = Int64(zip32)
print(zip)
if let xCrd = locationCreated[3] as? Double{
print(xCrd)
if let yCrd = locationCreated[4] as? Double{
print(yCrd)
newLocation = locationStore.createLocation(false, location: locationName, currentTemp: currentTemp, zipCode: zip, xCord: xCrd, yCord: yCrd)
if let index = locationStore.allLocations.indexOf(newLocation){
let indexPath = NSIndexPath(forRow: index, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
}
}
}
}
}
}
func createLocation(random: Bool, location: String, currentTemp: Double, zipCode: Int64, xCord: Double, yCord: Double ) -> Location{
if !random{
let newLocation = Location(random: false, locationNme: location, currentTmperature: currentTemp, locationZpCode: zipCode, xCrd: xCord, yCrd: yCord)
allLocations.append(newLocation)
return newLocation
}
LocationStore:
let newLocation = Location(random: true, locationNme: location, currentTmperature: currentTemp, locationZpCode: zipCode, xCrd: xCord, yCrd: yCord)
allLocations.append(newLocation)
return newLocation
}
Location:
init( locationName: String, currentTemperature: Double, locationZipCode: Int64, xCord: Double, yCord: Double){
self.locationName = locationName
self.currentTemperature = currentTemperature
self.locationZipCode = Int64(locationZipCode)
self.xCord = xCord
self.yCord = yCord
self.itemKey = NSUUID().UUIDString
super.init()
}
convenience init( random:Bool = false, locationNme: String, currentTmperature: Double, locationZpCode: Int64, xCrd: Double, yCrd: Double ){
if random {
let locations = ["Louisville", "Gainesville", "Austin", "San Francisco"]
//38.2527, 85.7585
//29.6516, 82.3248
// 30.2672, -97.7431
// 37.7749, -122.4194
let xCords = [38.2527, 29.6516, 30.2672, 37.7749 ]
let yCords = [-85.7585, -82.3248, -97.7431, -122.4194 ]
let zips = [40217, 32608, 77878, 46454]
let temps = [76.0, 101.3, 95.4, 68.5]
var idx = arc4random_uniform(UInt32(locations.count))
let randomLocation = locations[Int(idx)]
let randomLocationxCord = xCords[Int(idx)]
let randomLocationyCord = yCords[Int(idx)]
let randomZip = zips[Int(idx)]
idx = arc4random_uniform(UInt32(temps.count))
let randomTemp = temps[Int(idx)]
self.init ( locationName: randomLocation, currentTemperature: randomTemp, locationZipCode: Int64(randomZip), xCord: randomLocationxCord, yCord: randomLocationyCord)
}
else{
self.init( locationName: locationNme, currentTemperature: currentTmperature, locationZipCode: locationZpCode, xCord: xCrd, yCord: yCrd)
}
}
APIFinder:
func useAPI(zipCode: Int) -> NSArray{
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
let locationURL = NSURL( string: "http://api.openweathermap.org/data/2.5/forecast/city?zip=\(zipCode),us&APPID=74b78f4effe729b2a841cb35e3862d85")
let request = NSURLRequest(URL: locationURL!)
let task = session.dataTaskWithRequest(request) {
(data,response, error) -> Void in
if let locationData = data {
if let jsonString = NSString( data:locationData,encoding: NSUTF8StringEncoding){
let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding)
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String: AnyObject]
if let city = json["city"]{
if let name = city["name"]{
self.locationArray[0] = name!
self.locationArray[1] = zipCode
}
if let coord = city["coord"]{
if let xCord = coord!["lat"]{
self.locationArray[3] = xCord!
}
if let yCord = coord!["lon"]{
self.locationArray[4] = yCord!
}
}
}
if let list = json["list"]{
if let main = list[0]["main"]{
if let temp = main!["temp"]{
self.locationArray[2] = temp!
}
}
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
else if let requestError = error {
print("Error fetching weather data: \(requestError)")
}
else {
print("Unexpected error with request")
}
}
task.resume()
return locationArray
}
Output:
5
Test
0.0
0
0.0
0.0
5
Hartford
295.54
42328
37.45116
-86.909157
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
Solved:
The problem was that I needed to pass the LocationStore between the views in the segue. Such a dumb problem once the answer was shown.
Thanks for all of the help and suggestions.

Best way to convert JSON or other untyped data into typed classes in Swift?

I'm trying to parse out JSON into typed classes for safety/convenience, but it's proving very clunky. I wasn't able to find a library or even a post for Swift (Jastor is as close as I got). Here's a fabricated little snippet to illustrate:
// From NSJSONSerialization or similar and casted to an appropriate toplevel type (e.g. Dictionary).
var parsedJson: Dictionary<String, AnyObject> = [ "int" : 1, "nested" : [ "bool" : true ] ]
class TypedObject {
let stringValueWithDefault: String = ""
let intValueRequired: Int
let nestedBoolBroughtToTopLevel: Bool = false
let combinedIntRequired: Int
init(fromParsedJson json: NSDictionary) {
if let parsedStringValue = json["string"] as? String {
self.stringValueWithDefault = parsedStringValue
}
if let parsedIntValue = json["int"] as? Int {
self.intValueRequired = parsedIntValue
} else {
// Raise an exception...?
}
// Optional-chaining is actually pretty nice for this; it keeps the blocks from nesting absurdly.
if let parsedBool = json["nested"]?["bool"] as? Bool {
self.nestedBoolBroughtToTopLevel = parsedBool
}
if let parsedFirstInt = json["firstInt"] as? Int {
if let parsedSecondInt = json["secondInt"] as? Int {
self.combinedIntRequired = parsedFirstInt * parsedSecondInt
}
}
// Most succinct way to error if we weren't able to construct self.combinedIntRequired?
}
}
TypedObject(fromParsedJson: parsedJson)
There's a number of issues here that I'm hoping to work around:
It's extremely verbose, since I need to wrap every single property in a copy-pasted if-let for safety.
I'm not sure how to communicate errors when required properties are missing (as noted above). Swift seems to prefer (?) using exceptions for show-stopping problems (rather than pedestrian malformed data as here).
I don't know a nice way to deal with properties that exist but are the wrong type (given that the as? casting will fail and simply skip the block, it's not very informative to the user).
If I want to translate a few properties into a single one, I need to nest the let blocks proportional to the number of properties I'm combining. (This is probably more generally a problem with combining multiple optionals into one value safely).
In general, I'm writing imperative parsing logic when I feel like I ought to be able to do something a little more declarative (either with some stated JSON schema or at least inferring the schema from the class definition).
I do this using the Jastor framework:
1) Implement a Protocol that has a single function that returns an NSDictionary response:
protocol APIProtocol {
func didReceiveResponse(results: NSDictionary)
}
2) Create an API class that defines an NSURLConnection object that can be used as a Request URL for iOS's networking API. This class is created to simply return a payload from the itunes.apple.com API.
class API: NSObject {
var data: NSMutableData = NSMutableData()
var delegate: APIProtocol?
func searchItunesFor(searchTerm: String) {
// Clean up the search terms by replacing spaces with +
var itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+",
options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
var escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=music"
var url: NSURL = NSURL(string: urlPath)
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
println("Search iTunes API at URL \(url)")
connection.start()
}
// NSURLConnection Connection failed.
func connection(connection: NSURLConnection!, didFailWithError error: NSError!) {
println("Failed with error:\(error.localizedDescription)")
}
// New request so we need to clear the data object.
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
self.data = NSMutableData()
}
// Append incoming data.
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
self.data.appendData(data)
}
// NSURLConnection delegate function.
func connectionDidFinishLoading(connection: NSURLConnection!) {
// Finished receiving data and convert it to a JSON object.
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
delegate?.didReceiveResponse(jsonResult)
}
}
3) Create a class with associated properties that inherits from Jastor
NSDictionary response:
{
"resultCount" : 50,
"results" : [
{
"collectionExplicitness" : "notExplicit",
"discCount" : 1,
"artworkUrl60" : "http:\/\/a4.mzstatic.com\/us\/r30\/Features\/2a\/b7\/da\/dj.kkirmfzh.60x60-50.jpg",
"collectionCensoredName" : "Changes in Latitudes, Changes in Attitudes (Ultmate Master Disk Gold CD Reissue)"
}
]
}
Music.swift
class Music : Jastor {
var resultCount: NSNumber = 0
}
4) Then in your ViewController be sure to set the delegate to self and then make a call to the API's searchITunesFor() method.
var api: API = API()
override func viewDidLoad() {
api.delegate = self;
api.searchItunesFor("Led Zeppelin")
}
5) Implement the Delegate method for didReceiveResponse(). Jastor extends your class to set a NSDictionary of the results returned from the iTunes API.
// #pragma - API Delegates
func didReceiveResponse(results: NSDictionary) {
let music = Music(dictionary: results)
println(music)
}
Short version: Since init isn't allowed to fail, validation has to happen outside of it. Optionals seem to be the intended tool for flow control in these cases. My solution is to use a factory method that returns an optional of the class, and use option chaining inside it to extract and validate the fields.
Note also that Int and Bool aren't children of AnyObject; data coming from an NSDictionary will have them stored as NSNumbers, which can't be cast directly to Swift types. Thus the calls to .integerValue and .boolValue.
Long version:
// Start with NSDictionary since that's what NSJSONSerialization will give us
var invalidJson: NSDictionary = [ "int" : 1, "nested" : [ "bool" : true ] ]
var validJson: NSDictionary = [
"int" : 1,
"nested" : [ "bool" : true ],
"firstInt" : 3,
"secondInt" : 5
]
class TypedObject {
let stringValueWithDefault: String = ""
let intValueRequired: Int
let nestedBoolBroughtToTopLevel: Bool = false
let combinedIntRequired: Int
init(intValue: Int, combinedInt: Int, stringValue: String?, nestedBool: Bool?) {
self.intValueRequired = intValue
self.combinedIntRequired = combinedInt
// Use Optionals for the non-required parameters so
// we know whether to leave the default values in place
if let s = stringValue {
self.stringValueWithDefault = s
}
if let n = nestedBool {
self.nestedBoolBroughtToTopLevel = n
}
}
class func createFromDictionary(json: Dictionary<String, AnyObject>) -> TypedObject? {
// Validate required fields
var intValue: Int
if let x = (json["int"]? as? NSNumber)?.integerValue {
intValue = x
} else {
return nil
}
var combinedInt: Int
let firstInt = (json["firstInt"]? as? NSNumber)?.integerValue
let secondInt = (json["secondInt"]? as? NSNumber)?.integerValue
switch (firstInt, secondInt) {
case (.Some(let first), .Some(let second)):
combinedInt = first * second
default:
return nil
}
// Extract optional fields
// For some reason the compiler didn't like casting from AnyObject to String directly
let stringValue = json["string"]? as? NSString as? String
let nestedBool = (json["nested"]?["bool"]? as? NSNumber)?.boolValue
return TypedObject(intValue: intValue, combinedInt: combinedInt, stringValue: stringValue, nestedBool: nestedBool)
}
class func createFromDictionary(json: NSDictionary) -> TypedObject? {
// Manually doing this cast since it works, and the only thing Apple's docs
// currently say about bridging Cocoa and Dictionaries is "Information forthcoming"
return TypedObject.createFromDictionary(json as Dictionary<String, AnyObject>)
}
}
TypedObject.createFromDictionary(invalidJson) // nil
TypedObject.createFromDictionary(validJson) // it works!
I've also done the following to convert to/from:
class Image {
var _id = String()
var title = String()
var subTitle = String()
var imageId = String()
func toDictionary(dict dictionary: NSDictionary) {
self._id = dictionary["_id"] as String
self.title = dictionary["title"] as String
self.subTitle = dictionary["subTitle"] as String
self.imageId = dictionary["imageId"] as String
}
func safeSet(d: NSMutableDictionary, k: String, v: String) {
if (v != nil) {
d[k] = v
}
}
func toDictionary() -> NSDictionary {
let jsonable = NSMutableDictionary()
self.safeSet(jsonable, k: "title", v: self.title);
self.safeSet(jsonable, k: "subTitle", v: self.subTitle);
self.safeSet(jsonable, k: "imageId", v: self.imageId);
return jsonable
}
}
Then I simply do the following:
// data (from service)
let responseArray = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: nil) as NSArray
self.objects = NSMutableArray()
for item: AnyObject in responseArray {
var image = Image()
image.toDictionary(dict: item as NSDictionary)
self.objects.addObject(image)
}
If you want to POST the data:
var image = Image()
image.title = "title"
image.subTitle = "subTitle"
image.imageId = "imageId"
let data = NSJSONSerialization.dataWithJSONObject(image.toDictionary(), options: .PrettyPrinted, error: nil) as NSData
// data (to service)
request.HTTPBody = data;