2

I'm creating an app for iOS devices which will monitor price of one stock. I've successfully created a function which collets the data from an API and prints out the current value of stock.

The problem is that when I run the app one (current) price gets printed and it stays like that all the time.

My question is how do I make that function repeat itself every specified amount of time?

This is my viewDidLoad and is the function I want to repeat every couple of seconds.

Code:

import Cocoa
class ViewController: NSViewController {
let baseURL = "url"
@IBOutlet weak var bitcoinValue: NSTextField!
override func viewDidLoad() {
    super.viewDidLoad()
        getJSON { (usdPrice) -> Void in
            let usdPriceText = usdPrice.description
            self.bitcoinValue.stringValue = usdPriceText 
            print(usdPrice)   
        }  
}
func getJSON(completion: (Double) -> Void) {
    let url = NSURL(string: baseURL)
    let request = NSURLRequest(URL: url!)
    let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
    let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in

        if error == nil{
            let swiftyJSON = JSON(data: data!)
            let usdPrice = swiftyJSON["bpi"]["USD"]["rate"].doubleValue
            completion(usdPrice)
        } else {
            print("There was an error!")
        }
    } 
    task.resume()
    }
}
Anton O.
  • 653
  • 7
  • 27
Anton N
  • 35
  • 2
  • 9

1 Answers1

10

You can use NSTimer.

following is a example

//in your viewdidload
var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)


// must be internal or public. 
func update() {
// your repeating function
}
Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • Where should I put my function and can I just use name of my function insted of new update function? – Anton N Jan 08 '16 at 23:31
  • @Anton N You should put you're function the class section and yes you can change the name of the function, but then you would have to change the name of the selector. – Anton O. Jan 09 '16 at 00:07
  • I get this error message: "2016-01-09 01:48:40.124 Bitfo[2885:397879] -[Bitfo.ViewController getJSON]: unrecognized selector sent to instance 0x600000100510 2016-01-09 01:48:40.124 Bitfo[2885:397879] -[Bitfo.ViewController getJSON]: unrecognized selector sent to instance 0x600000100510 2016-01-09 01:48:40.125 Bitfo[2885:397879]" – Anton N Jan 09 '16 at 00:56
  • did u declare any method named getJSON ? – Ratul Sharker Jan 09 '16 at 02:03
  • @Anton N Can you show us the code you have so we can correct it? – Anton O. Jan 09 '16 at 04:27
  • @AntonO. I'll place it in the question – Anton N Jan 09 '16 at 13:47
  • You can't call `getJSON(completion:)` using NSTimer or any other control that uses the target-action pattern. The method signature does not match. You can call it from within `update()` as Ratul proposed though. – Aaron Brager Jan 09 '16 at 23:10
  • Aaron Brager is right, you can't pass multiple param in NSTimer, it should be none/one, if there is one, it should be the timer itself, check the [following](http://stackoverflow.com/questions/23312583/passing-a-block-in-selector) If you were intended to use thread then more felxibility could be achieved using [following](https://developer.apple.com/library/ios/technotes/tn2109/_index.html). – Ratul Sharker Jan 10 '16 at 03:24