0

I want to keep calling following function for 'n' number of times, I'll write that code later. Now am facing a problem.

func keepHighlighting(myLbl : UILabel)
{
    myLbl.text = "hi"
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{           
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for : indexPath) as! TableViewCell

    let mySelector = #selector(self.keepHighlighting(duaLbl : cell.tempLbl))
    let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: mySelector, userInfo: nil, repeats: true);      
    timer.fire()

    return cell
}

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

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
AbaEesa
  • 998
  • 1
  • 6
  • 22

2 Answers2

0

You've misunderstood selectors. self.keepHighlighting(duaLbl : cell.tempLbl) is a function call. A selector is the name of a method (it's technically a message, but those generally resolve into a method call).

You cannot pass parameters via a selector. The method you're calling from a timer should have the signature void someMethod(_ timer: Timer). For that, the selector would be #selector(someMethod). (That compiles down to someMethod:, but you generally won't have to deal with that fact.)

Depending on what you're trying to do here, there are several approaches, but it's not quite clear what you're trying to do. My recommendation, though, is to put this logic into the cell itself rather than trying to manage it from the controller. It is generally much easier to have each cell manage its own behaviors than try to have the controller keep track of every cell and tweak it.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I understand what is method call & what is selector. I just want to keep calling that function after specified interval & that function accepts params. – AbaEesa Jan 16 '17 at 14:56
  • You cannot pass parameters with a selector. They do not carry data. They are (roughly) names of methods. You will have to do this another way. I recommend doing it inside of the cell; then you wouldn't need to pass a parameter (since the cell can just refer to its own label). – Rob Napier Jan 16 '17 at 15:01
  • I've huge code in that function, for asking question I just simplified that function, don't just think that's just a single line. – AbaEesa Jan 16 '17 at 15:03
  • That shouldn't matter if the signature doesn't change. But in any case, you cannot pass arbitrary parameters to a Timer. If you need to, you must redesign somehow because you cannot do it. What you can do is load the information into the `userInfo` dictionary parameter and your method can unload it if you want that. (But the method itself still must have a `f(_ timer: Timer)` signature. That is required for timers.) – Rob Napier Jan 16 '17 at 15:07
0

You have to annotate your method with @objc and use the Timers userInfoto parse arguments:

@objc func keepHighlighting(timer:Timer)
{
    let myLbl = timer.userInfo as! UILabel
    myLbl.text = "hi"
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for : indexPath) as! UITableViewCell

    let mySelector = #selector(self.keepHighlighting)
    let timer = Timer.scheduledTimer(timeInterval: 0.4, target: self, selector: mySelector, userInfo: cell.tempLbl, repeats: true);
    timer.fire()

    return cell
}
shallowThought
  • 19,212
  • 9
  • 65
  • 112