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
The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.
viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.
viewDidLoad method is called only once in ViewController lifecycle. The reason retrieveMessage() is called in viewDidLoad because it's adding observer to start listening for received and sent message.
viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears. In this simple app, that means it is only called once.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With