提问人:iOS Newbie 提问时间:11/15/2023 最后编辑:iOS Newbie 更新时间:11/15/2023 访问量:51
最新的 Swift/Xcode 并出现错误尚未解决 [已关闭]
Lastest Swift/Xcode and getting an error not figured out yet [closed]
问:
我有这个:
“没有类型注释的表达式类型不明确” 对于此代码片段:
if let rootViewController = self.window?.rootViewController as? UINavigationController {
if let controller = controller as? UIViewController {
rootViewController.present(controller, animated: true, completion: {
// Completion code, if needed
})
} else {
print("Error: The 'controller' is not a UIViewController")
}
} else {
print("Error: The root view controller is not a UINavigationController")
}
“nil requires a contextual type”错误,您可以在显示视图控制器时提供完成闭包
和
我们用作为?UIViewController,在显示控制器之前将控制器强制转换为 UIViewController。这确保了 Swift 知道预期的类型。 我们还添加了额外的错误处理,以检查控制器是否不是 UIViewController,并在该情况下打印错误消息。
但到目前为止,我还没有奏效。我使用的是最新的 macOS、Xcode、Xcode 工具和 Swift。
谢谢
整体:
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
// Handle the notification response here
let userInfo = response.notification.request.content.userInfo
// Check if it's a job request notification
if let notificationType = userInfo["notificationType"] as? String,
notificationType == "jobRequest" {
// Retrieve the state value from userInfo
if let stateValue = userInfo["state"] as? Int {
// Unwrap and convert customerUid to String
if let customerUid = userInfo["customerUid"] as? String {
// Create the jobRequest dictionary with the state value
let jobRequestData: [String: Any] = [
"customerUid": customerUid,
"fullname": userInfo["fullname"] as? String ?? "",
"username": userInfo["username"] as? String ?? "",
"businessName": userInfo["businessName"] as? String ?? "",
"businessId": userInfo["businessId"] as? String ?? "",
"address": userInfo["address"] as? String ?? "",
"firstDateAndTimes": userInfo["firstDateAndTimes"] as? String ?? "",
"secondDateAndTimes": userInfo["secondDateAndTimes"] as? String ?? "",
"thirdDateAndTimes": userInfo["thirdDateAndTimes"] as? String ?? "",
"timeConstraints": userInfo["timeConstraints"] as? String ?? "",
"jobDescription": userInfo["jobDescription"] as? String ?? "",
"timestamp": userInfo["timestamp"] as? Timestamp ?? Timestamp(date: Date()),
"employeeUid": userInfo["employeeUid"] as? String ?? "",
"state": stateValue
]
let jobRequest = JobRequest(customerUid: customerUid, dictionary: jobRequestData)
let controller = CustomerJobRequestController(jobRequest: jobRequest)
// Check if the root view controller is a UINavigationController
if let rootViewController = self.window?.rootViewController as? UINavigationController {
rootViewController.present(controller, animated: true)
} else {
// Handle the case where the root view controller is not a UINavigationController
// You can show an alert or take appropriate action here
print("Error: Root view controller is not a UINavigationController")
}
} else {
// Handle the case where customerUid is not a String or is nil
print("Error: Unable to retrieve customerUid as String")
}
}
}
completionHandler()
}
答: 暂无答案
评论
controller
JobRequest
CustomerJobRequestController
UIViewController
userNotificationCenter