2015-12-22 108 views

回答

0

你沒有給出代碼的例子,所以我會寫在摘要中。您需要在完成NSURLSession對象內調用與您的對象一起工作的方法。例如,它可能看起來像這樣:

// This is your object 
struct SomeData { 
     var someValue: Int = 0 
    } 

// This is class that use it 
class Foo { 
    var someData: SomeData 

    init() { 
     someData = SomeData() 
     requestData() 
    } 

    // This is your function that need to wait for the request 
    func doActionWithData() { 
     print(someData.someValue) 
    } 

    // This is request 
    func requestData() { 
     // Make request with your params 
     let request = NSMutableURLRequest(...) 
     ... 
     // For example you do it like this 
     NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data, response, error) in 
      // here you have data for your object you can get it from 
      // response and then call function to work with it 
      self.someData.someValue = ... 
      self.doActionWithData() 
     }) 
    } 
} 
相關問題