Simply want to read the launch options in Swift.
This is my old obj-C code which worked fine:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *URL = [launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
if (URL)
this is what I think the Swift code should look like:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if let options = launchOptions
{
let URL: NSURL = options(UIApplicationLaunchOptionsURLKey) as String
but it gives the error:
'(NSString!) -> $T2' is not identical to '[NSObject : AnyObject]'
A casting error? but having difficulty casting it properly and can't find links for how to do it.
Swift 3:
In Swift 3,
launchOptions
is a dictionary of type[UIApplicationLaunchOptionsKey: Any]?
, so you'd access the value like this:Since the key type is
UIApplicationLaunchOptionsKey
, you can abbreviate theenum
type as simply.url
:The value associated with that key is a
URL
though, and not aString
. Also, the key might not be present in the dictionary, so you should use conditional castingas?
instead of normal casting.In Swift, you want to do:
Swift 2:
In your code,
launchOptions
is a dictionary of type[NSObject: AnyObject]?
, so you'd want to access the value like this:The value associated with that key is an
NSURL
though, and not aString
. Also, the key might not be present in the dictionary, so you should use conditional castingas?
instead of normal casting.In Swift, you want to do: