Ajp
Ajp

Reputation: 333

Swift Playgrounds - use of unresolved identifier 'self'

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?

Upvotes: 0

Views: 1567

Answers (3)

cetcet
cetcet

Reputation: 2130

Put @objc before func tapBlurButton...

Upvotes: 0

Leo
Leo

Reputation: 39

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?

Upvotes: -1

Jack Lawrence
Jack Lawrence

Reputation: 10772

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.

Upvotes: 0

Related Questions