Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift映射变量消失_Swift_Mkmapview_Mapkit_Viewdidload_Mkmapviewdelegate - Fatal编程技术网

Swift映射变量消失

Swift映射变量消失,swift,mkmapview,mapkit,viewdidload,mkmapviewdelegate,Swift,Mkmapview,Mapkit,Viewdidload,Mkmapviewdelegate,我正试图建立一个地图,放大一个位置。一切都应该很好,但地图意外地变成了零,我不知道为什么 @IBOutlet weak var map: MKMapView! var numberOfAnnotations = 0 override func viewDidLoad() { numberOfAnnotations = 0 var locationManager: CLLocationManager = CLLocationManager() map = MKMa

我正试图建立一个地图,放大一个位置。一切都应该很好,但地图意外地变成了零,我不知道为什么

@IBOutlet weak var map: MKMapView!

var numberOfAnnotations = 0

override func viewDidLoad() {

    numberOfAnnotations = 0

    var locationManager: CLLocationManager = CLLocationManager()

    map = MKMapView()

    println(map)

    locationManager.delegate = self

    locationManager.requestWhenInUseAuthorization()

    locationManager.startUpdatingLocation()

    map.showsUserLocation = true

    var location = locationManager.location.coordinate

    var span = MKCoordinateSpanMake(0.01, 0.01)

    println(location.latitude)
    println(location.longitude)

    var region = MKCoordinateRegion(center: location, span: span)

    println(region.center.longitude)
    println(region.span.latitudeDelta)
    println(map)

    map.setRegion(region, animated: true)

    println(map)
    println(map.description)

    var longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotationToMap:")
    // duration required to place a pin is .75 seconds
    longPress.minimumPressDuration = 0.75


    map.addGestureRecognizer(longPress)

    map.delegate = self

    super.viewDidLoad()

}
它生成的打印行如下所示,它在最后一个println(map.description)上崩溃,并且在此之前在map.addGestureRecognizer上崩溃

<MKMapView: 0x15fb0400; frame = (0 0; 0 0); clipsToBounds = YES; layer = <CALayer: 0x15fb0350>>
42.7881728503759
-86.1060027215381
-86.1060027215381
0.01
<MKMapView: 0x15fb0400; frame = (0 0; 0 0); clipsToBounds = YES; layer =<CALayer: 0x15fb0350>>
nil
fatal error: unexpectedly found nil while unwrapping an Optional value

42.7881728503759
-86.1060027215381
-86.1060027215381
0.01
无
致命错误:在展开可选值时意外发现nil

有人知道为什么会发生这种情况吗?

在顶部调用这个方法super.viewDidLoad()在viewDidLoad,为什么需要这个line map=MKMapView()?有些行不是必需的,但无论如何,请尝试遵循这些建议。是否将
super.viewDidLoad()
放在方法的顶部?在它之后设置
map.delegate=self
?将您的地图声明为
strong
而不是
weak
?super.viewDidLoad在底部被调用,因为我们有一个选项卡系统,如果我们没有,它将被地图覆盖。初始化变量需要map=MKMapView()。您是否尝试了我所说的…?是的,将super.viewdiload移到顶部没有任何作用,删除map的初始化使它在map.showsUserLocation=true使用map变量时崩溃