Access control issues for Selector methods in Swift

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.