Ios 为什么我的CLLocation实例数组是空的?
我目前正在尝试构建一种计算我当前位置和其他硬编码位置之间的距离(以英尺为单位)的方法。我最终将在表视图中将这些距离显示为字符串。以下是我正在采取的步骤: 1) 我有一个硬编码的字典库,每个字典都有一个“纬度”和“经度”键值对 2) 要从结构中提取此信息,请执行以下操作:Ios 为什么我的CLLocation实例数组是空的?,ios,xcode,swift,cllocationmanager,cllocationdistance,Ios,Xcode,Swift,Cllocationmanager,Cllocationdistance,我目前正在尝试构建一种计算我当前位置和其他硬编码位置之间的距离(以英尺为单位)的方法。我最终将在表视图中将这些距离显示为字符串。以下是我正在采取的步骤: 1) 我有一个硬编码的字典库,每个字典都有一个“纬度”和“经度”键值对 2) 要从结构中提取此信息,请执行以下操作: struct BarDetials { // other properties .... var lat: CLLocationDegrees? var long CLLocationDegrees? init(
struct BarDetials {
// other properties
....
var lat: CLLocationDegrees?
var long CLLocationDegrees?
init(index: Int) {
// initialize other properties
// ...
lat = specificBarDetail["latitude"] as! CLLocationDegrees!
long = specificBarDetail["longitude"] as! CLLocationDegrees!
}
}
3) 我使用另一个结构从这些坐标创建CLLocation实例数组,如下所示:
struct ConstructLocationsToCompare {
var latitude : [CLLocationDegrees?] = []
var longitude : [CLLocationDegrees?] = []
var barLocations : [CLLocation?] = []
init(){
for index in 0...21 {
var data = BarDetails(index: index)
if data.lat != nil {
latitude.append(data.lat!)
}
if data.long != nil {
longitude.append(data.long!)
}
barLocations[index] = CLLocation(latitude: latitude[index]!, longitude: longitude[index]!)
}
}
}
4) 然后在MasterViewController()中计算距离
var latestLocation:任何对象,var currentLocation:CLLocation!,和var distconsensingarray:[String?]=[]都是我的MasterViewController()类的属性
5) 最后,要显示我与每个位置的距离(在my-cellforrowatinexpath中):
***我的DistingArray始终为空,因此我出现致命错误,应用程序崩溃。我该如何解决这个问题?我声明var latestLocation:AnyObject是事实吗,var currentLocation:CLLocation!,和var distconsensingarray:[String?]=[]我的MasterViewController()类的属性错误实践?如果没有,这些属性是否需要转换为不同的类型/以不同的方式声明?在
print()
:println(self.latestLocation)
->println(“latestLocation:\(self.latestLocation)”)中包含一些信息确实很有帮助。
。另外,要打印换行符,只需使用“\n”
:而不是多个println(“”
只需println(“\n\n\n”)
。这是基本的“C”元素。我看不出你在哪里添加任何东西到DistancentingArray
。此外,您不应该从可选项中获得失败错误,您必须检查所有在任何情况下都可能返回nil的选项。在print()
:println(self.latestLocation)
->println(“latestLocation:\(self.latestLocation)”)中包含一些信息确实很有帮助。另外,要打印换行符,只需使用“\n”
:而不是多个println(“”
只需println(“\n\n\n”)
。这是基本的“C”元素。我看不出你在哪里添加任何东西到DistancentingArray
。此外,您不应该从可选项中获得失败错误,您必须检查所有可能在任何情况下返回nil的可选项。
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
// Get latest location and store it as a property of MasterViewController
if self.latestLocation != nil {
self.latestLocation = locations[locations.count - 1]
// Print out the latest locaiton for debuging
println("Latest Location")
println("---------------")
println(self.latestLocation)
// Create an instance of ContructLocationsToCompare to get all locaiton data
var getBarLocations = ConstructLocationsToCompare()
// Get loop to calculate the distance you are away from each Bar
var distanceDoubleArray : [Double?] = []
for index in 0...21 {
var distanceBetween : CLLocationDistance = self.latestLocation!.distanceFromLocation(getBarLocations.barLocations[index])
var distanceInFeet = distanceBetween * 3.28084
distanceDoubleArray[index] = distanceInFeet
self.distancesStringArray.append( String(format: "%6.0 ft", distanceDoubleArray[index]!))
}
}
//println(distanceBetween) or get error if it exists
if error != nil {
println("Error: " + error.localizedDescription)
return
}
if placemarks.count > 0 {
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
})
}
println("")
println("")
println("List of Locations")
println("=================")
println(self.distancesStringArray)
println("")
println("")
println("")
println("")
if self.distancesStringArray[indexPath.row]!.isEmpty == true{
futureCell.distanceAway?.text = "-" // I crash at this line ^^^^^^
} else {
futureCell.distanceAway?.text = self.distancesStringArray[indexPath.row]
}