我正在尝试使用以下代码从 CloudKit 获取当前用户的名字:
func getUserFirstName() -> String{
var firstNameFromFunction: String?
var currentuserID : CKRecordID?
container.fetchUserRecordID(completionHandler: {
userID, error in
if ((error == nil)) {
self.currentuserID = userID!
}
})
container.discoverUserInfo(withUserRecordID: currentuserID!, completionHandler: {userInfo, error in
let firstNameFromFunction = (userInfo!.displayContact?.givenName)!
})
return firstNameFromFunction!
}
但是在执行时,currentUserID 为 nil,并创建了致命错误。有谁知道为什么 currentUserID 为零?