gpt4 book ai didi

ios - 如何根据 swiftyJson 获取的 json 在 swift 中创建我的类的对象?

转载 作者:行者123 更新时间:2023-11-29 01:12:49 34 4
gpt4 key购买 nike

我有一个类(class)如下:

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()
}
}

我还有一个如下所示的 json:

[{"_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.

现在我想获取所有 json 条目并为每个条目创建 SingleRequest 对象。

到目前为止,我在此类中创建了一个方法:

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)


}
}
}

现在有 2 个问题:

1) 正如您在上面看到的,我将这两个字段留空了:

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

那是因为我不知道如何从我的 json 中引用 long/lat 值,因为它们嵌入在 coordinates 字段中...

如何填写?

2) 这个函数会创建所需的对象吗?或者我应该更改声明以标记一些返回值:

class func getAllRequests()

?谢谢!

最佳答案

对于您的第一个问题,您需要先从 user["coordinates"] 中获取数组然后向下转换为数组,user["coordinates"] as? Array<Double>

对于你的第二个问题,它应该返回一个 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
}

关于ios - 如何根据 swiftyJson 获取的 json 在 swift 中创建我的类的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35542965/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com