-1

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 ?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
f1rstsurf
  • 637
  • 1
  • 6
  • 22
  • It's completely unclear what this has to do with `viewDidLoad`. It's impossible to decipher here what is supposed to be happening and what is actually happening, and why you think what is supposed to be happening should be happening. – nhgrif Jun 08 '15 at 12:43
  • thank's for the -1, i just want to call my method in viewdidload, because at the moment where the view is open, i want to create my cards, but only if my json return 1 for the "succes" key. – f1rstsurf Jun 08 '15 at 12:52
  • write this code in viewwilappear also – Memon Irshad Jun 08 '15 at 12:55
  • 1
    Your comment doesn't clarify anything. If you want to call it in `viewDidLoad`, then call it in `viewDidLoad`. If that's not working, take some time and effort to explain exactly what's happening with as much detail as possible and explain exactly how this differs from what you want with absolutely as much detail as possible. – nhgrif Jun 08 '15 at 12:55

1 Answers1

0

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

Community
  • 1
  • 1
Shamas S
  • 7,507
  • 10
  • 46
  • 58