0

I am using Swift playgrounds trying to make an interactive app in which the user can click on a button to perform an action.

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))

func tapBlurButton(_ sender: UITapGestureRecognizer) {
print("Please Help!")
}

It is giving me the error:

'use of unresolved identifier self'

. How can I fix this in Swift 3?

Joe
  • 8,868
  • 8
  • 37
  • 59
Ajp
  • 333
  • 1
  • 2
  • 9

3 Answers3

0

That code isn't going to work unless it's inside a class that inherits from NSObject.

If it is in a class and you're still getting that error, it's probably because tapGesture is a property. Properties generally can't use self during initialization.

Jack Lawrence
  • 10,664
  • 1
  • 47
  • 61
0

Put @objc before func tapBlurButton...

cetcet
  • 2,147
  • 2
  • 15
  • 15
-1

Put those code in class.

class SomeClass: NSObject {
    [your code with self]
}

Because there's nothing to say self and playground.

And How to reference Swift Playground itself?

Community
  • 1
  • 1
Leo
  • 39
  • 5