简体   繁体   中英

how can I create objects of my class in swift based on json fetched by swiftyJson?

I have a class as follows:

import Foundation
import MapKit

class SingleRequest: NSObject, MKAnnotation {

var title: String?
let created: String
let discipline: String
let coordinate: CLLocationCoordinate2D


var items = NSMutableArray()


init(title: String, created: String, discipline: String, coordinate: CLLocationCoordinate2D) {
    self.title = title
    self.created = created
    self.discipline = discipline
    self.coordinate = coordinate

    super.init()
}
}

I also have a json that looks like:

[{"_id":"56c9d44fbcb42e075f7d49b1",
"username":"Ms. Brynlee Quitzon DDS",
"photo":"photo.jpg",
"number":"one",
"description":"Maiores rerum beatae molestiae autem. Voluptatem magni aspernatur est voluptas.",
"__v":0,
"updated_at":"2016-02-21T15:14:23.123Z",
"created_at":"2016-02-21T15:14:23.116Z",
"location":{
"type":"Point",
"coordinates":[5.300567929507009,44.04127433959841]}
},
etc.

and now I want to fetch all json entries and create SingleRequest object for each of them.

So far I created a method in this class:

class func getAllRequests() {
    print("getAllRequests")
    RestApiManager.sharedInstance.getRequests { json in
        let results = json//["username"]
        for (index: String, subJson: JSON) in results {
            let user: AnyObject = JSON.object



            var title = user["description"]
            let created = user["created_at"]
            let discipline = user["number"]

            let latitude = (user[""]).doubleValue
            let longitude = (user[""]).doubleValue
            let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

            return SingleRequest(title: title, created: created!, discipline: discipline!, coordinate: coordinate)


        }
    }
}

And now 2 questions:

1) as you can see above, I left those two fields empty:

let latitude = (user[""]).doubleValue
let longitude = (user[""]).doubleValue

that's because I don't know how to refer to the long/lat values from my json, since they are embeded in the coordinates field...

How can I fill it?

2) will this function create needed objects? or should I for example change the declaration to mark some return value:

class func getAllRequests()

? Thanks!

For your first question, you need to first get the array out of user["coordinates"] and then downcast it to Array, user["coordinates"] as? Array<Double> user["coordinates"] as? Array<Double>

For your second question, it should return an array of SingleRequest, Array<SingleRequest>

class func getAllRequests() -> Array<SingleRequest> {
    var requests: Array<SingleRequest> = [] 
    RestApiManager.sharedInstance.getRequests { json in
         let results = json//["username"]
         for (index: String, subJson: JSON) in results {
              let user: AnyObject = JSON.object

              var title = user["description"]
              let created = user["created_at"]
              let discipline = user["number"]

              guard let coordinates = user["coordinates"] as? Array<Double> else { print("no lat/long") }

              let latitude = coordinates[0]
              let longitude = coordinates[1]
              let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

              requests.append(SingleRequest(title: title, created: created!, discipline: discipline!, coordinate: coordinate))
        }
    }

    return requests
}
var singleRequestsArray = [SingleRequest]()


class func getAllRequests() {

    RestApiManager.sharedInstance.getRequests { json in

        for (index: String, subJson: JSON) in son {

            let user: AnyObject = subjson.object

            let title = user["description"]
            let created = user["created_at"]
            let discipline = user["number"]

            var coordinate = CLLocationCoordinate2D()

            if let coordinates = user["coordinates"].array{

                let latitude = coordinates.first?).doubleValue
                let longitude = coordinates.array.last?).doubleValue
               coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
            }

        singleRequestsArray.append(SingleRequest(title: title, created: created!, discipline: discipline!, coordinate: coordinate))

        }
    }
}

Hope this will help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM