按位置打印数组显示为空
Print Array By Location shows up empty
objective是这样的:
- 计算数组中每一项的纬度和经度
- 将纬度和经度附加到数组
- 以最大距离打印数组中的所有项目。
以下是我提取数据的方式:
import Foundation
import SwiftyJSON
class restaurant {
//Json
var id: Int!
var name: String!
var address: String!
var city: String!
var logo: String!
var state: String!
var postKey: String!
//Second
var ids: Int!
var names: String!
var aAddress: String!
var citys: String!
var logos: String!
var states: String!
var distance : Double = 0
//Importing data from server. Bring moto. Make sure you are able to get details in postman prior to adding any other variables
init(json: JSON) {
self.id = json["id"].int
self.name = json["dispensary_name"].string
self.address = json["street_address"].string
self.logo = json["logo"].string
self.city = json["city"].string
self.state = json["state"].string
}
init(postKey: String, postData: Dictionary<String, AnyObject>) {
self.postKey = postKey
// self.ids = postData[self.id]?.int
self.names = postData[self.name]?.string
self.aAddress = postData[self.address]?.string
self.citys = postData[self.city]?.string
self.logos = postData[self.logo]?.string
self.states = postData[self.state]?.string
}
}
这是我的数组:
//加载餐厅列表。
func loadRestaurants() {
Helpers.showActivityIndicator(activityIndicator, view)
APIManager.shared.getRestaurants { (json) in
if json != nil {
self.restaurants = []
//Adds all items into the array list dist then goes through them all.
if let listDis = json["restaurant"].array {
for item in listDis {
let address = item["street_address"].string! + ", " + item["city"].string! + ", " + item["state"].string!
self.getLocation(address, { (dis) in
self.disLocation = dis
})
//snap = item, snapshot = listdict
if let snapValue = item.array as? [String:AnyObject],
let venueLat = self.disLocation?.coordinate.latitude,
let venueLong = self.disLocation?.coordinate.longitude {
print("snapValue ,", snapValue)
let distance = self.calculateDistance(userlat: self.userLatt, userLon: self.userLonn, venueLat: venueLat, venueLon: venueLong)
if distance <= 15 {
let key = item.string
let restaurant = Restaurant(postKey: key!, postData: snapValue)
restaurant.distance = distance
self.restaurants.append(restaurant)
print("restaurant :", snapValue)
}
}
}
if self.restaurants.count == 0 {
let alert = UIAlertController(title: "Oh No!!!", message: "We Only Deliver To New Jersey", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
}))
self.present(alert, animated: true, completion: nil)
}
self.restaurants.sort {
[=11=].distance < .distance
}
}
self.restaurants.reloadData()
Helpers.hideActivityIndicator(self.activityIndicator)
}
}
}
我不确定我应该在这里做什么。任何建议都会有所帮助。
项目输出:
"state" : "New Jersey",
"city" : "Highland Park",
"id" : 3,
"dispensary_name" : "Down South Taste",
"street_address" : "4422 knotwood ct",
"zip_Code" : 56430,
"phone" : "444-334-0888",
您用于 Restaurant 对象(第二个)的 init 方法显然是错误的,并且会导致运行时崩溃,因为您正在解包 nil 值。
self.names = postData[self.name]?.string
name
在这里为零,但声明为
var name: String!
我真的不明白你在阅读字典时试图用属性而不是字符串常量来实现什么?
也许可以做一些类似的事情
let nameKey = "dispensary_name"
...
self.names = postData[nameKey]?.string
如果我理解正确,你的程序不会崩溃,这告诉我们从未调用构造函数。我的猜测是周围 if 语句中的条件失败了,并且条件的第一部分在您调用 json 项上的 .array
并将结果类型转换为字典时看起来是错误的
if let snapValue = item.array as? [String:AnyObject], ...
.array.
在这里看起来不对,虽然我不知道你的 json 格式,但你之前访问 item
属性 作为字典,不是吗 item
那是你的字典。
希望这能帮助你前进。
objective是这样的:
- 计算数组中每一项的纬度和经度
- 将纬度和经度附加到数组
- 以最大距离打印数组中的所有项目。
以下是我提取数据的方式:
import Foundation
import SwiftyJSON
class restaurant {
//Json
var id: Int!
var name: String!
var address: String!
var city: String!
var logo: String!
var state: String!
var postKey: String!
//Second
var ids: Int!
var names: String!
var aAddress: String!
var citys: String!
var logos: String!
var states: String!
var distance : Double = 0
//Importing data from server. Bring moto. Make sure you are able to get details in postman prior to adding any other variables
init(json: JSON) {
self.id = json["id"].int
self.name = json["dispensary_name"].string
self.address = json["street_address"].string
self.logo = json["logo"].string
self.city = json["city"].string
self.state = json["state"].string
}
init(postKey: String, postData: Dictionary<String, AnyObject>) {
self.postKey = postKey
// self.ids = postData[self.id]?.int
self.names = postData[self.name]?.string
self.aAddress = postData[self.address]?.string
self.citys = postData[self.city]?.string
self.logos = postData[self.logo]?.string
self.states = postData[self.state]?.string
}
}
这是我的数组: //加载餐厅列表。
func loadRestaurants() {
Helpers.showActivityIndicator(activityIndicator, view)
APIManager.shared.getRestaurants { (json) in
if json != nil {
self.restaurants = []
//Adds all items into the array list dist then goes through them all.
if let listDis = json["restaurant"].array {
for item in listDis {
let address = item["street_address"].string! + ", " + item["city"].string! + ", " + item["state"].string!
self.getLocation(address, { (dis) in
self.disLocation = dis
})
//snap = item, snapshot = listdict
if let snapValue = item.array as? [String:AnyObject],
let venueLat = self.disLocation?.coordinate.latitude,
let venueLong = self.disLocation?.coordinate.longitude {
print("snapValue ,", snapValue)
let distance = self.calculateDistance(userlat: self.userLatt, userLon: self.userLonn, venueLat: venueLat, venueLon: venueLong)
if distance <= 15 {
let key = item.string
let restaurant = Restaurant(postKey: key!, postData: snapValue)
restaurant.distance = distance
self.restaurants.append(restaurant)
print("restaurant :", snapValue)
}
}
}
if self.restaurants.count == 0 {
let alert = UIAlertController(title: "Oh No!!!", message: "We Only Deliver To New Jersey", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
}))
self.present(alert, animated: true, completion: nil)
}
self.restaurants.sort {
[=11=].distance < .distance
}
}
self.restaurants.reloadData()
Helpers.hideActivityIndicator(self.activityIndicator)
}
}
}
我不确定我应该在这里做什么。任何建议都会有所帮助。
项目输出:
"state" : "New Jersey",
"city" : "Highland Park",
"id" : 3,
"dispensary_name" : "Down South Taste",
"street_address" : "4422 knotwood ct",
"zip_Code" : 56430,
"phone" : "444-334-0888",
您用于 Restaurant 对象(第二个)的 init 方法显然是错误的,并且会导致运行时崩溃,因为您正在解包 nil 值。
self.names = postData[self.name]?.string
name
在这里为零,但声明为
var name: String!
我真的不明白你在阅读字典时试图用属性而不是字符串常量来实现什么?
也许可以做一些类似的事情
let nameKey = "dispensary_name"
...
self.names = postData[nameKey]?.string
如果我理解正确,你的程序不会崩溃,这告诉我们从未调用构造函数。我的猜测是周围 if 语句中的条件失败了,并且条件的第一部分在您调用 json 项上的 .array
并将结果类型转换为字典时看起来是错误的
if let snapValue = item.array as? [String:AnyObject], ...
.array.
在这里看起来不对,虽然我不知道你的 json 格式,但你之前访问 item
属性 作为字典,不是吗 item
那是你的字典。
希望这能帮助你前进。