1

I'm very new to swift and am having trouble with calling this function again on the 6th line after a delay, Xcode is telling me that

Argument of '#selector' does not refer to an '@objc' method, property, or initializer

I know what the problem is and have tried searching around but haven't been able to make anything work.

@objc func attemptToIdentify(_ user: String) {
    if manager.status == .connected {
        socket.emit("identify", user) //This functions correctly
    } else {
        print(manager.status, "... will attempt subscription in 1 second")
        self.perform(#selector(attemptToIdentify(user)), with: nil, afterDelay: 1)
    }
}
rmaddy
  • 307,833
  • 40
  • 508
  • 550
b.stevens.photo
  • 723
  • 8
  • 17
  • Using matt's suggestions I changed line 6 to 'self.perform(#selector(attemptToIdentify), with: user, afterDelay: 1)' and everything is working now – b.stevens.photo Oct 18 '18 at 21:30
  • Great but I would still suggest that using `perform:with:afterDelay:` at all is a bad idea. There is a Swift way to do a delay and that's not it. – matt Oct 18 '18 at 21:34

1 Answers1

2

The problem is that attemptToIdentify(user) is a call. This is not the place for a call; you need a selector, i.e. the name of a function. So just put attemptToIdentify. If you need to pass something, you can pass it in the with parameter.

Even better, don't use perform:afterDelay: at all. If the idea is to add a delay to a call, just use asyncAfter (or my delay encapsulation of it).

matt
  • 485,702
  • 82
  • 818
  • 1,064