Mapview nil 关闭,在 StackO 上看到了主题。但没有帮助我
Mapview nil closure, saw topics on StackO. but didn't help me
我已经阅读了用户让我检查的这个主题
但这对我没有帮助,mapView 仍然为零。
我也读过这个,但没有帮助 http://www.apeth.com/swiftBook/ch04.html#SECinstanceRefs
谁能请 review/see 我的代码并帮助我,而不是链接到另一个根本没有帮助的主题?
我在 class 驱动程序中有这个功能
private static let _instance = Driver();
static var Instance: Driver {
return _instance;
}
func getAllDriversLocations(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController;
var location : [CLLocation] = []
Account.Instance.dbRef.child("driverLocations").observeSingleEvent(of: .value, with: { (snapshot) in
if let data = snapshot.value as? NSDictionary {
if let loca = data as? [String: [String:Any]]{
let keys = loca.keys
let sortedKeys = Array(keys).sorted(by: { [=10=] > }) // highest will be the first
_ = sortedKeys[0]
//let intHighPlus = Int(highestKey)! + 1
for index in 0...sortedKeys.count-1{
let stringIndex = "0\(index+1)"
if let indexChecked = loca[stringIndex]{
if let doubleLatString = (indexChecked["latitude"]){
let latDouble = (doubleLatString as! NSString).doubleValue
if let doubleLongString = (indexChecked["longitude"]){
let longDouble = (doubleLongString as! NSString).doubleValue
location.append(CLLocation(latitude: (latDouble), longitude: (longDouble)))
}
}
}
}
if location.count > 0{
vc.placeAllCabPlacemarks(array: location)
}
}
}
})
}
所以在这里我调用了 placeAllCabPlacemarks,位置数组在 placeAllCabPlacemarks 函数中正确获取,但后来我发现 mapView 在这里为 nil 的问题:
@IBOutlet weak var myMap: MKMapView?
override func viewDidLoad() {
super.viewDidLoad()
myMap?.delegate = self
initializeLocationManager()
Driver.Instance.getAllDriversLocations()
}
func placeAllCabPlacemarks(array: [CLLocation]){
//print("locs: \(array)")
let arraySize = array
for index in 0...arraySize-1 {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: array[index].coordinate.latitude, longitude: array[index].coordinate.longitude)
print("map \(myMap)") //Here it's nil
myMap?.addAnnotation(annotation as MKAnnotation)
}
}
我确实检查了所有内容,还检查了 Internet 和 Whosebug 上的一些项目。
有人可以检查为什么这不起作用吗?
主要问题是mapView是nil...
所以我在之前 post 中链接到的答案的问题...
第一个问题是我在以下位置使用单例:
Driver.Instance.getAllDriversLocations()
我将其更改为:
let driver = Driver() //instance of the class driver
driver.getAllDriversLocations(callBack: self)
第二个问题是这段代码:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController;
我将该代码更改为:
func getAllDriversLocations(callBack : ViewController){
let vc = callBack
}
我希望有一天我能帮助某人解决这个问题,而不是像以前的 post 我链接到的那样让事情变得更糟..
我已经阅读了用户让我检查的这个主题
但这对我没有帮助,mapView 仍然为零。 我也读过这个,但没有帮助 http://www.apeth.com/swiftBook/ch04.html#SECinstanceRefs 谁能请 review/see 我的代码并帮助我,而不是链接到另一个根本没有帮助的主题?
我在 class 驱动程序中有这个功能
private static let _instance = Driver();
static var Instance: Driver {
return _instance;
}
func getAllDriversLocations(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController;
var location : [CLLocation] = []
Account.Instance.dbRef.child("driverLocations").observeSingleEvent(of: .value, with: { (snapshot) in
if let data = snapshot.value as? NSDictionary {
if let loca = data as? [String: [String:Any]]{
let keys = loca.keys
let sortedKeys = Array(keys).sorted(by: { [=10=] > }) // highest will be the first
_ = sortedKeys[0]
//let intHighPlus = Int(highestKey)! + 1
for index in 0...sortedKeys.count-1{
let stringIndex = "0\(index+1)"
if let indexChecked = loca[stringIndex]{
if let doubleLatString = (indexChecked["latitude"]){
let latDouble = (doubleLatString as! NSString).doubleValue
if let doubleLongString = (indexChecked["longitude"]){
let longDouble = (doubleLongString as! NSString).doubleValue
location.append(CLLocation(latitude: (latDouble), longitude: (longDouble)))
}
}
}
}
if location.count > 0{
vc.placeAllCabPlacemarks(array: location)
}
}
}
})
}
所以在这里我调用了 placeAllCabPlacemarks,位置数组在 placeAllCabPlacemarks 函数中正确获取,但后来我发现 mapView 在这里为 nil 的问题:
@IBOutlet weak var myMap: MKMapView?
override func viewDidLoad() {
super.viewDidLoad()
myMap?.delegate = self
initializeLocationManager()
Driver.Instance.getAllDriversLocations()
}
func placeAllCabPlacemarks(array: [CLLocation]){
//print("locs: \(array)")
let arraySize = array
for index in 0...arraySize-1 {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: array[index].coordinate.latitude, longitude: array[index].coordinate.longitude)
print("map \(myMap)") //Here it's nil
myMap?.addAnnotation(annotation as MKAnnotation)
}
}
我确实检查了所有内容,还检查了 Internet 和 Whosebug 上的一些项目。 有人可以检查为什么这不起作用吗? 主要问题是mapView是nil...
所以我在之前 post 中链接到的答案的问题...
第一个问题是我在以下位置使用单例:
Driver.Instance.getAllDriversLocations()
我将其更改为:
let driver = Driver() //instance of the class driver
driver.getAllDriversLocations(callBack: self)
第二个问题是这段代码:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController;
我将该代码更改为:
func getAllDriversLocations(callBack : ViewController){
let vc = callBack
}
我希望有一天我能帮助某人解决这个问题,而不是像以前的 post 我链接到的那样让事情变得更糟..