search

Home  >  Q&A  >  body text

xcode - iOS: How to use swift to add an infinite loop and a thread with time interval?

It can be implemented simply to this extent:
Define a bool type variable. This variable will be judged every time it loops. If it is true, print(666) will be printed, and the thread will sleep for 1s, and then repeat the cycle. .

I tried GCD's .concurrentPerform, but it can only execute a certain number, it's not really an infinite loop, and I don't know how to set the time interval.
There is also an NStimer method on the Internet, but I don't know which framework has been imported, and There is no NSTimer class (Swift project).


I tried Cadisplaylink and the problem has been solved, but when initializing it as a global variable,

let displaylink = CADisplayLink(target: self,
   selector: #selector(step))

An error will be reported, why?

巴扎黑巴扎黑2768 days ago776

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-31 10:34:00

    I confirmed in the comments that the subject of the question is target: self An error was reported here.

    The following are the questions after the question:

    Oh yes, the controller has not been initialized at that time, thank you. Can you also ask me again what is the use of this target: variable? I only know that most targets fill in self, but I don’t know what the purpose of filling in self is, it is setting Agent?

    A simple understanding is where the following parameter selector belongs.
    Generally, selector points to a certain method in a certain class. When let displaylink = CADisplayLink(target: self, selector: #selector(step)) is written in the same class, naturally target is self.

    reply
    0
  • Cancelreply