23

You can't write to self.state in the subclass unless you import UIGestureRecognizerSubclass.h as indicated here.

In a Swift environment, I'm confused how I'd go about importing this. I tried import UIGestureRecognizerSubclass.h, and without the .h, but I still can't write to self.state.

How would I accomplish this?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

2 Answers2

91

The Swift equivalent is simply:

import UIKit.UIGestureRecognizerSubclass

That imports the appropriate header.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    Nice! Didn't think you could do that :) – chrs Aug 06 '15 at 07:53
  • The simplest solutions are usually the best! Thanks – SomaMan Sep 04 '15 at 12:25
  • 1
    This is also a better solution for Swift code inside a custom framework because if you add `#import ` to your framework's umbrella header, it will be imported to all clients of the framework, and you probably don't want that. – dalton_c Jul 06 '17 at 15:29
15

You need to have or create a -Bridging-Header.h file to import objc headers such as the one you want. The import line looks like this:

#import <UIKit/UIGestureRecognizerSubclass.h>

If you don't already have a bridge header file in your app, the easiest way to get one is to add an objc class to your project, and xcode will ask if you want one, then creates the file and ties it into the settings for you. You can then delete the objc class.

Everything in that header file is automatically made available to your Swift code, no need to add any import lines in your swift files.

Jim T
  • 2,480
  • 22
  • 21