最新的 Swift/Xcode 并出现错误尚未解决 [已关闭]

Lastest Swift/Xcode and getting an error not figured out yet [closed]

提问人:iOS Newbie 提问时间:11/15/2023 最后编辑:iOS Newbie 更新时间:11/15/2023 访问量:51

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

8天前关闭。

我有这个:

“没有类型注释的表达式类型不明确” 对于此代码片段:

 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()
}
iOS Swift Xcode

评论

0赞 HangarRash 11/15/2023
在完整的上下文中显示此代码。我从您发布的代码中没有得到您提到的任何错误。同时显示 的声明。controller
0赞 lorem ipsum 11/15/2023
注释掉部分,直到找到错别字
0赞 iOS Newbie 11/15/2023
添加了整个方法,看看它是否建立在你这边。
0赞 HangarRash 11/15/2023
@iOSNewbie 一旦我添加了我自己的虚拟结构和扩展类,您的函数就会编译而不会出现任何错误。JobRequestCustomerJobRequestControllerUIViewControlleruserNotificationCenter

答: 暂无答案