Ios 使用错误处理优化CloudKit登录应用程序启动。如何更好地处理可选链接?Swift 3.0/Xcode 8.1
有没有更干净、更快捷的解决方案来处理我下面代码中出现的可选链接?我正在自定义函数runCKsetup()中为CloudKit访问设置用户:Ios 使用错误处理优化CloudKit登录应用程序启动。如何更好地处理可选链接?Swift 3.0/Xcode 8.1,ios,swift,cloudkit,Ios,Swift,Cloudkit,有没有更干净、更快捷的解决方案来处理我下面代码中出现的可选链接?我正在自定义函数runCKsetup()中为CloudKit访问设置用户: func runCKsetup(){ container.requestApplicationPermission(.userDiscoverability){(状态,错误)在 保护错误==nil else{ 如果let error=错误为?n错误{ 如果让errorDictionary:AnyObject=error.userInfo作为?Dictiona
func runCKsetup(){
container.requestApplicationPermission(.userDiscoverability){(状态,错误)在
保护错误==nil else{
如果let error=错误为?n错误{
如果让errorDictionary:AnyObject=error.userInfo作为?Dictionary作为AnyObject{
让localizedDescription=errorDictionary[“NSLocalizedDescription”]!作为!字符串!
如果localizedDescription!==“此操作已被速率限制”{
//创建警报视图
let alert=UIAlertController(标题:“网络错误”,消息:localizedDescription!,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“连接测试”,样式:UIAlertActionStyle.default){(操作)在
self.runCKsetup()
})
self.present(警报、动画:true、完成:nil)
}否则{
//创建警报视图
let alert=UIAlertController(标题:“登录到iCloud”,消息:localizedDescription!,首选样式:UIAlertControllerStyle.alert)
addAction(UIAlertAction(标题:“Ok”,样式:UIAlertActionStyle.default){(操作)在
})
self.present(警报、动画:true、完成:nil)
}
}
}
返回
}
如果状态==CKApplicationPermissionStatus.grated{
self.container.fetchUserRecordID{(recordID,错误)位于
保护错误==nil else{
self.presentMessageAlert((错误?.localizedDescription)!,标题:“错误”,按钮:“确定”)
返回}
guard let recordID=recordID else{return}
self.container.DiscoveryUserIdentity(在
//对用户名执行一些操作:例如,打印(\(info?.nameComponents?.givenName)\(info?.nameComponents?.familyName))
})
}
}
}
}
func runCKsetup() {
container.requestApplicationPermission(.userDiscoverability) { (status, error) in
guard error == nil else {
if let error = error as? NSError {
if let errorDictionary: AnyObject = error.userInfo as? Dictionary<String, AnyObject> as AnyObject? {
let localizedDescription = errorDictionary["NSLocalizedDescription"]! as! String!
if localizedDescription! == "This operation has been rate limited" {
// Create an alert view
let alert = UIAlertController(title: "Network Error", message: localizedDescription!, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Test for Connection", style: UIAlertActionStyle.default) { (action) in
self.runCKsetup()
})
self.present(alert, animated: true, completion: nil)
} else {
// Create an alert view
let alert = UIAlertController(title: "Sign in to iCloud", message: localizedDescription!, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default) { (action) in
})
self.present(alert, animated: true, completion: nil)
}
}
}
return
}
if status == CKApplicationPermissionStatus.granted {
self.container.fetchUserRecordID { (recordID, error) in
guard error == nil else {
self.presentMessageAlert((error?.localizedDescription)!, title: "Error", buttonTitle: "Ok")
return }
guard let recordID = recordID else { return }
self.container.discoverUserIdentity(withUserRecordID: recordID, completionHandler: { (info, fetchError) in
//do something with the users names: e.g. print("\(info?.nameComponents?.givenName) \(info?.nameComponents?.familyName)")
})
}
}
}