Fructose Fructose - 2 years ago 139
Swift Question

Can create a selector for viewWillAppear but not for viewDidLoad

I'm not seeing why this will compile:

let viewWillAppearSelector = #selector(UIViewController.viewWillAppear(_:))

But not this:

let viewDidLoadSelector = #selector(UIViewController.viewDidLoad())

The error is

"Use of instance member 'viewDidLoad' on type UIViewController; did
you mean to use a value type 'UIViewController' instead?

Why does the latter not complile but the former does?

Update: I changed it to this and now it compiles:

let viewDidLoadSelector = #selector(UIViewController.viewDidLoad)

But I'm not 100% why that is

Answer Source

If a method does not take any parameters you need to omit the parentheses. In a future version of Swift your code will become an error.


let viewDidLoadSelector = #selector(UIViewController.viewDidLoad)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download