Fructose Fructose - 6 months ago 31
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

HAS HAS
Answer

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.

Try

let viewDidLoadSelector = #selector(UIViewController.viewDidLoad)

instead.

Comments