Search code examples
swiftgestureviewdidload

Call method ViewDidload Swift


i currently developpe an app which request data from a webservice, and one the datas are retrieve, i call a method to create some "swype" card like tinder.

The problem is, i do not success to call my method in the viewDidload, this is my code :

if(success == 1) {
    NSLog("Recherche OK");
    //Call the swype method     
} 

This is my method :

func fetchData(int: Int, completion: (()->())?) {



    let newCard = Model()
    //ttp://thecatapi.com/api/images/get?format=src&type=png



    if let url = NSURL(string: "http://test.com/api/images/get?format=src&type=png") {
        if let data = NSData(contentsOfURL: url) {
            newCard.image = UIImage(data: data)
            newCard.content = "test"
            newCard.desc = "test"
            self.data.append(newCard)
            NSLog("fetch new data")
        }

Have you got an idea ?


Solution

  • Please read more about getting NSData from a network call. Following is from a Apple Documentation

    Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

    Instead, for non-file URLs, consider using the dataTaskWithURL:completionHandler: method of the NSSession class. See URL Loading System Programming Guide for details.

    Also I noticed that the url you are showing redirects, so maybe that is the problem. Instead of trying to load it in a UIImage, you can create a UIWebview and show the image there.

    You can find Swift related help regarding that in answer here