Today, we wrote a view with Swift, adding a gesture to the view as follows:
12 |
panGestureRecognizer = UIPanGestureRecognizer(target: self, action: "beginDragged:" ) addGestureRecognizer(panGestureRecognizer) |
Run the next program, and then crash. The crash log is as follows:
1 |
[**.SwipeCardView beginDragged:]: unrecognized selector sent to instance 0x125e5bc10 |
And I have defined the begindragged: method in the Swipecardview class as follows:
123 |
private func beginDragged(gestureRecognizer: UIPanGestureRecognizer) { // .... } |
Since I do not want to expose the begindragged: method, I define it as a private method. The definition of the method is normal, and the selector method of the gesture is also set to normal, but the exception of the method not found is reported. That might be the problem with access rights.
We know that selector is the product of objective-c, which is used to find the implementation of a corresponding method at runtime as a key value. A objective-c method is defined by the Objc_method end body, which declares the following:
12345 |
struct objc_method {      sel method_name objc2_unavailable; // Method name      char *method_types objc2_unavailable;      imp method_imp objc2_unavailable; // method implementation } |
This requires that the method referenced by selector must be visible to the OBJC runtime. While Swift is a static language, a class that inherits from NSObject is visible to the OBJC runtime by default, but if the method is decorated by the private keyword, the method is not visible to the OBJC runtime by default. So it leads to the above exception: the runtime does not find the begindragged of the Swipecardview class: method.
So, we have to expose the private adornment method to the runtime. The right thing to do is to add @objc keyword to the private front so that it's OK.
123 |
@objc private func beginDragged(gestureRecognizer: UIPanGestureRecognizer) { // .... } |
It is also important to note that if our class is a pure swift class and not inherited from NSObject, the @objc modifier is required for use in selector, regardless of whether the method is private or internal or public.
Access control issues for Selector methods in Swift