title: Swift: return value every x seconds
body: <p>I'm trying to generate random string every 10 seconds. I put this function in a class in another file and will call the function in another view controller. But now I'm not getting any output  when I call it. How to can I fix this code</p>
  <pre><code> class Data{
  
      static let instance = Data()
  
      func randomString(of length: Int){
         let letters = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;
         var s = &quot;&quot;
         for _ in 0 ..&lt; length {
             s.append(letters.randomElement()!)
          
             print(&quot;\(s) = I'm in randomString Func&quot;)
         }
         DispatchQueue.main.asyncAfter(deadline: .now() + 10.0) { [weak self] in
             self?.randomString(of: 5)
             }
     }
  
  
   }
  </code></pre>
  <p>in a view controller I put it under a button action and call it with this code</p>
  <pre><code> Button(action: {
                  info = Data.instance.randomString(of:5)
                  print(info)
  
              }, label: {
                  Text (&quot;PRINT&quot;)
                      .font(.callout)
                      .foregroundColor(Color.primary)
              })
  </code></pre>