2016-03-03 62 views
0

我對iOS開發新的REST API,我下面這個教程:https://grokswift.com/simple-rest-with-swift/,我不知道爲什麼下面的代碼總是返回「佔位」,我不能看到錯誤控制檯輸出:錯誤使用消耗迅速在iOS

import Foundation 

class Resolver{ 
    func doSomething() -> String{ 
     var result = "the placeholder" 
     print("inside doSomething") 
     let postEndpoint: String = "http://jsonplaceholder.typicode.com/posts/1" 
     guard let url = NSURL(string: postEndpoint) else { 
      print("Error: cannot create URL") 
      return "error here" 
     } 

     let urlRequest = NSURLRequest(URL: url) 

     let config = NSURLSessionConfiguration.defaultSessionConfiguration() 

     let session = NSURLSession(configuration: config) 
     print("another thing") 

     let task = session.dataTaskWithRequest(urlRequest, completionHandler: { 
      (data, response, error) in 
      guard let responseData = data else { 
       print("Error: did not receive data") 
       return 
       //return "error here" 
      } 
      print("hereeeeeee ############") 
      guard error == nil else { 
       print("error calling GET on /posts/1") 
       print(error) 
       return 
       //return "error here" 
      } 
      // parse the result as JSON, since that's what the API provides 
      let post: NSDictionary 
      do { 
       post = try NSJSONSerialization.JSONObjectWithData(responseData, 
        options: []) as! NSDictionary 
      } catch { 
       print("error trying to convert data to JSON") 
       return 
       //return "error here" 
      } 
      // now we have the post, let's just print it to prove we can access it 
      print("The post is: " + post.description) 
      result = post.description 

      // the post object is a dictionary 
      // so we just access the title using the "title" key 
      // so check for a title and print it if we have one 
      if let postTitle = post["title"] as? String { 
       print("The title is: " + postTitle) 
      } 

     }) 
     task.resume() 

     return result 
    }  

} 

這是我的info.plist:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>CFBundleIdentifier</key> 
    <string></string> 
    <key>CFBundleExecutable</key> 
    <string></string> 
    <key>CFBundleInfoDictionaryVersion</key> 
    <string>6.0</string> 
    <key>LSApplicationCategoryType</key> 
    <string></string> 
    <key>CFBundleName</key> 
    <string></string> 
    <key>CFBundleDisplayName</key> 
    <string></string> 
    <key>CFBundleVersion</key> 
    <string></string> 
    <key>NSAppTransportSecurity</key> 
    <dict> 
     <key>NSAllowsArbitraryLoads</key> 
     <true/> 
     <key>NSExceptionDomains</key> 
     <dict> 
      <key>httpbin.org</key> 
      <dict> 
       <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> 
       <true/> 
      </dict> 
      <key>jsonplaceholder.typicode.com </key> 
      <dict> 
       <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> 
       <true/> 
      </dict> 
     </dict> 
    </dict> 
    <key>CFBundleShortVersionString</key> 
    <string></string> 
    <key>CFBundleGetInfoString</key> 
    <string></string> 
</dict> 
</plist> 

當我運行測試我看到這樣的輸出:

Test Suite 'RestTest' started at 2016-03-03 15:38:49.364 
Test Case '-[FoodTrackerTests.RestTest testResolver]' started. 
inside doSomething 
another thing 
// I want to see the result: the placeholder 
Test Case '-[FoodTrackerTests.RestTest testResolver]' passed (0.061 seconds). 
Test Suite 'RestTest' passed at 2016-03-03 15:38:49.425. 
    Executed 1 test, with 0 failures (0 unexpected) in 0.061 (0.062) seconds 
Test Suite 'Selected tests' passed at 2016-03-03 15:38:49.426. 
    Executed 1 test, with 0 failures (0 unexpected) in 0.061 (0.063) seconds 

爲什麼結果變量不包含post.description值?

+0

你有沒有嘗試添加'task'內斷點?爲什麼永遠不會被調用?你的錯誤也不會丟失。也許這是因爲你正在運行測試並且在異步API調用之前完成測試? – brandonscript

+0

我試着在操場上運行你的代碼,它不會產生輸出,就像你發現的一樣。但是當我在真機上運行它時,它馬上就可以正常工作。 – emrys57

+0

@remus你是對的。測試總是在http調用完成之前完成。我不知道你是否必須發佈答案,以便將其標記爲已接受,或者如果我必須自己這樣做。 – PedroHidalgo

回答

0

正如評論所說,它看起來像測試完成返回前的HTTP調用(因爲HTTP請求是異步),因此試運行結束之前,你是不是看到了API調用的結果。

看一看XCAsyncTestCase和設置您的測試,以等待異步回調來代替。

0

doSomething()方法異步呼叫結束之前返回"the placeholder"值。這就是你看到它的原因。