简体   繁体   English

Swift 3 '[UIApplicationLaunchOptionsKey : Any]? 不可转换为 '[String : NSString]'

[英]Swift 3 '[UIApplicationLaunchOptionsKey : Any]?' is not convertible to '[String : NSString]'

I have a TVOS app that has been converted form Swift 2 to Swift 3 and I am getting the following error.我有一个已从 Swift 2 转换为 Swift 3 的 TVOS 应用程序,但出现以下错误。 I am unsure how to silence it.我不确定如何让它静音。

'[UIApplicationLaunchOptionsKey : Any]?' '[UIApplicationLaunchOptionsKey : Any]? is not convertible to '[String : NSString]'不可转换为 '[String : NSString]'

It is showing up in this piece of code它出现在这段代码中

appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL

        if let launchOptions = launchOptions as? [String: AnyObject] {
            for (kind, value) in launchOptions {
                appControllerContext.launchOptions[kind] = value
            }
        }

在此处输入图片说明

ADDED:添加:

 /* Copyright (C) 2015 Hani Hamrouni. All Rights Reserved. */ import UIKit import TVMLKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate, TVApplicationControllerDelegate { // MARK: Properties var window: UIWindow? var appController: TVApplicationController? //change the link to your host url static let TVBaseURL = "http://google.com" static let TVBootURL = "\\(AppDelegate.TVBaseURL)js/application.js" // MARK: UIApplication Overrides func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. window = UIWindow(frame: UIScreen.main.bounds) /* Create the TVApplicationControllerContext for this application and set the properties that will be passed to the `App.onLaunch` function in JavaScript. */ let appControllerContext = TVApplicationControllerContext() /* The JavaScript URL is used to create the JavaScript context for your TVMLKit application. Although it is possible to separate your JavaScript into separate files, to help reduce the launch time of your application we recommend creating minified and compressed version of this resource. This will allow for the resource to be retrieved and UI presented to the user quickly. */ if let javaScriptURL = URL(string: AppDelegate.TVBootURL) { appControllerContext.javaScriptApplicationURL = javaScriptURL } appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL if let launchOptions = launchOptions { for (kind, value) in launchOptions { appControllerContext.launchOptions[kind.rawValue] = value as AnyObject } } appController = TVApplicationController(context: appControllerContext, window: window, delegate: self) return true } // MARK: TVApplicationControllerDelegate func appController(_ appController: TVApplicationController, didFinishLaunching options: [String: Any]?) { print("\\(#function) invoked with options: \\(options)") } func appController(_ appController: TVApplicationController, didFail error: Error) { print("\\(#function) invoked with error: \\(error)") let title = "Error Launching Application" //error message let message = error.localizedDescription let alertController = UIAlertController(title: title, message: message, preferredStyle:.alert ) self.appController?.navigationController.present(alertController, animated: true, completion: { () -> Void in // ... }) } func appController(_ appController: TVApplicationController, didStop options: [String: Any]?) { print("\\(#function) invoked with options: \\(options)") } }

You'd better work with [UIApplicationLaunchOptionsKey : Any] as it is.您最好按原样使用[UIApplicationLaunchOptionsKey : Any]

How is this?这怎么样?

    if let launchOptions = launchOptions {
        for (kind, value) in launchOptions {
            appControllerContext.launchOptions[kind.rawValue] = value
        }
    }

UPDATED更新

Seems the type of the property launchOptions of TVApplicationControllerContext is [String: Any] , so you have no need to cast with as AnyObject .似乎launchOptions的属性launchOptionsTVApplicationControllerContext[String: Any] ,因此您无需使用as AnyObject

try this code please and tell me what happen.请尝试此代码并告诉我会发生什么。

appControllerContext.launchOptions["BASEURL"] = AppDelegate.TVBaseURL

        if let launchOptions = launchOptions as? [String: Any] {
            for (kind, value) in launchOptions {
                appControllerContext.launchOptions[kind] = value
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM