OK this is rather odd to me, can someone explain to me why handleDismiss
can only be called one way?
consider the following:
import UIKit
class MenuLanucher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
//[...] stuff
let menuItems: [MenuCellSetting] = {
return [
MenuCellSetting(name: "Exit Application", imageName: "hamburger", ontap: {
print("it is exit")
MenuLanucher.handleDismiss() //<-- 2. this is illegal: 'instance member 'handleDismiss' cannot be used on type 'MenuLanucher'; did you mean to use a value of this type instead?'
}),
MenuCellSetting(name: "Create", imageName: "gear", ontap: {
print("it is job")
self?.HandleDismiss() //<-- 2. illegal : 'Cannot use optional chaining on non-optional value of type '(MenuLanucher) -> () -> (MenuLanucher)''
}),
MenuCellSetting(name: "Cancel", imageName: "gear", ontap: {
print("it is nothing")
perform(#selector(MenuLanucher.handleDismiss)) //<-- 3. crashes on run time 'unrecognized selector sent to class'
})
]
}()
//[...] yet
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
menuItems[indexPath.item].ontap()
handleDismiss() //<--1. works
}
@objc func handleDismiss(){
print("dismiss works")
}
}
class MenuCellSetting: NSObject {
let name: String
let imageName: String
let ontap: ()->Void
init(name: String, imageName: String, ontap: @escaping ()->Void){
self.name = name
self.imageName = imageName
self.ontap = ontap
}
}
in this example
- fails: at runtime saying 'unrecognized selector sent to class'
- fails: at compile saying 'instance member 'handleDismiss' cannot be used on type 'MenuLanucher'; did you mean to use a value of this type instead?'
- works
my question is: why is the difference? what is going on?
EDIT: self.?handleDismiss() also fails (see image)