我正在尝试根据某些参数恢复所有动物对象。首先,我需要从解析和名称中检索它们的位置,但由于我导入多个并使用地理编码器,我使用的是字符串,而不是数组。因此,我不是将导入的信息附加到数组中,而是改变变量。我会发生的是查询将通过第一个对象然后运行retrieveLocation方法,然后继续从parse导入的下一个对象,但它导入所有内容然后运行方法,所以最后我只获得1个对象应该导入多少。
let query = PFQuery(className: "Animals")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) in
if(error == nil){
for object in objects!{
if let addressobj = object["Add"] as? NSDictionary{
if let address = addressobj["address"] as? String{
self.addr = address
print("sdfadsf \(self.addr)")
}
}
if let name = object["Name"] as? String{
self.impname = name
print("rturrty \(self.impname)")
self.retrieveLocation()
}
}
}
}
func retrieveLocation(){
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(addr, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error)
}
if let placemark = placemarks?.first {
let coordinates = PFGeoPoint(location: placemark.location)
if(whatever is true){
append the name and address into an array. This is the part where I just get repeats of the LATEST imported object.
}
}
})
}
答案 0 :(得分:0)
如果您使用局部变量并将此局部变量传递给retrieveLocation的实现,该实现将字符串作为参数retrieveLocation(address: String)
let query = PFQuery(className: "Animals")
query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) in
if(error == nil){
for object in objects!{
if let addressobj = object["Add"] as? NSDictionary{
if let address = addressobj["address"] as? String{
let newAddress = address
print("sdfadsf \(self.addr)")
}
}
if let name = object["Name"] as? String{
self.impname = name
print("rturrty \(self.impname)")
self.retrieveLocation(newAdress)
}
}
}
}
func retrieveLocation(address: String){
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if((error) != nil){
print("Error", error)
}
if let placemark = placemarks?.first {
let coordinates = PFGeoPoint(location: placemark.location)
if(whatever is true){
append the name and address into an array. This is the part where I just get repeats of the LATEST imported object.
}
}
})
}
问题似乎是在geocodeAddresString方法中使用self.addr
时,for循环已经完成,因此覆盖了self.addr
单独持有的所有先前值。通过使用局部变量,每次执行时都可以使用唯一值来geocodeAddressString