I am trying to call the function in ViewController
from appdelegate
in Swift?
In Appdelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var first: FirstViewController!
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
FirstViewController.fileurl(url)
first.fileurl(url)
return true
}
}
In FirstViewController.swift
import UIKit
class FirstViewController: UIViewController {
var externalFile: NSURL!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func fileurl(url: NSURL){
externalFile = url
println("externalFile = \(externalFile)")
}
}
In Appdelegate.swift
, I call the FirstViewController.fileurl()
and also try to call first.fileurl()
.
When I call FirstViewController.fileurl(url)
, it show Cannot invoke 'fileurl' with an argument list of type '(NSURL)'.
When I call first.fileurl(url)
, it crash and the error log is fatal error: unexpectedly found nil while unwrapping an Optional value.
Did I missing something ? Thanks in advance.