قالب وردپرس درنا توس
Home / IOS Development / Gesture Recognizers in Swift

Gesture Recognizers in Swift



Like many things, much has not been changed when using UIGestureRecognizer in Swift. Let's compare the first two lens-C:

  -    (  Invalid )   viewDidLoad ; 
  {
      [super   viewDidLoad];    UITAPGestureRecognizer [19659000] *  gestureRecognizer    =    [[UITapGestureRecognizer   lloc]    initWithTarget: med    action:   @selector  (handleTap 🙂 ]; 
      [selfview   addGestureRecognizer: gestureRecognizer]; 
} 

  -    (  tomrom ) handleTap: (UIGestureRecognizer    * ) connoisseur; 
  {
      CGPoint    Location    =    [Recipient   LocationInView: Self.view]; [1
9659000] NSString
* Message = [NSString stringWithFormat: @ " Du Lost By: % ] + " NSStringFromCGPoint (placement)]; UIAlertController [19659000] * alertCont false = [UIAlertController alertControllerWithTitle: @ " Du Tapped " Message: Message preferredStyle: UIAlertControllerStyleAlert]; [alertController addAction: [UIAlertAction actionWithTitle: @ " Avvis " style: UIAlertActionStyleCancel therapist: null]] [self-confidence[19659003] presentViewController animated: YES completion: nil]; }
  [19659000]] [19659000]] [19659000]] [19659000]] [19659000]] [19659000]] [19659000]] [19659000]]]    gestureRecognizer    =    UITAPGestureRecognizer (target:    self     action:    " handleTap: [19659042]" ) 
      self  .view. addGestureRecognizer (gestureRecognizer) 
} 

  FUNC    handleTap  (gestureRecognizer: [19659003] UIGestureRecognizer)    {
      la    alertController    =    UIAlertController (title:        message:    " Du [19659003] dropped    by      (gestureRecognizer.locationInView (  by yourself  .The view) )  "    preferredStyle:    .alert) [19659009] alertController.addAction (UIAlertAction (title:   "  Remove [19659047] "    Style:    .Cancel,    Treats:    {   _ [19659078] in  })) 
      Self  .presentViewController               Completion:    nil ) 
 } 

While there are similar, there are small differences that potentially can crash.

Your natural tendency may be to make the action method private. There is no reason why something outside of the current file must call this method; it is an implementation detail of how detectives work. Unfortunately, this will cause the following crash while driving:

  *** Completing the app because of uncaught exception & NS ;validArgumentException & # 39 ;, reason: - [GestureRecognizers.SwiftGestureRecognizerExampleViewController handleTap:]: Unknown selects sent to example 0x7fec3a7ac8b0 & # 39; `

In Objective-C, which lacks methodology as a language function, the method can be considered private because it is not in the header file. Unfortunately, your application crashes by meeting this requirement in Swift. The section about exposing Swift interface in the Swift with Cocoa and User C Guide, explains why. First, explain:

If your class inherits from a Objective-C class, the compiler adds the [ @objc ] attribute for you.

However, it will be explained later in a note:

The compiler does not automatically enter the @ objc attribute for declarations labeled with the private access modifier.

Our class inherits from UIViewController which is an objective C object, but as default private methods do not get the @objc modifier added to them. You can add the modifier, but I have been told that this can lead to the name of collisions. Unfortunately, I could not find a good source of this requirement.

This behavior is surprising to me because it violates Swift's principle of design for safety. There is no compiler warning, in fact there is no warning at all, just a crash at runtime (FYI: rdar: // 21594714).

Another interesting thing to note is that Swift has to judge by appearance, Swift has no idea about selectors. The action argument is just a string. As a matter of fact, it is automatically converted to a Swift Selector because it complies with the StringLiteralConvertible protocol. If you are interested, NSHipster has written more about literal convertibles.

Although it is trivial to convert Objective-C to Swift detection code, it is also one of these APIs, where a small fraction of the Objective-C inheritance shines through cracks. Not a big deal, but definitely something to be aware of to prevent roadbreakers.


Questions or comments? Find us at twitter or send a problem on github




Source link