Swift预先允许覆盖

时间:2017-12-11 00:44:38

标签: swift xcode permissions cllocationmanager uialertcontroller

在我获取用户位置的函数中,我创建了预授权,以便在授权消息之前发出警报,以免失去从用户那里获得必要权限的机会。

func getCurrentLocation() {
// Init LocationManager
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = 
kCLLocationAccuracyThreeKilometers;

let alert = UIAlertController(title: "Allow Loction Services?",
message: "This allows the app to your loction",
preferredStyle: .alert)

let ok = UIAlertAction(title: "Allow", style: .default, handler: { 
(action) -> Void in
self.showHUD("loading...")

if locationManager.responds(to: 
#selector(CLLocationManager.requestWhenInUseAuthorization)) {
locationManager.requestAlwaysAuthorization()
}}
self.hideHUD()
)
}



let cancel = UIAlertAction(title: "Not Now", style: .destructive, 
handler: { (action) -> Void in })

alert.addAction(ok); alert.addAction(cancel)
present(alert, animated: true, completion: nil)

locationManager.startUpdatingLocation()
}

在以下部分:

let ok = UIAlertAction(title: "Allow", style: .default, handler: { (action) -> Void in self.showHUD("loading...")

错误弹出我们说“没有更多上下文的表达类型是不明确的”

1 个答案:

答案 0 :(得分:0)

Swift编译器给出了这个错误,因为语句self.hideHUD()超出了标记完成处理程序块的大括号。

我怀疑上面的线上没有两个花括号,你需要一个花括号,而另一个后隐藏HUD:

.......
        locationManager.requestAlwaysAuthorization()
    }
    self.hideHUD()
}
........