2017-03-04 68 views
-5
enter code here 
@IBOutlet weak var textfield: UITextView! 

@IBOutlet weak var label: UILabel! 


var sentimentURL = "https://api.havenondemand.com/1/api/sync/analyzesentiment/v2?text=this+is+lethargic&apikey=e55bc0ff-70a5-4270-b464-aaaf3dfcc8de&text=" 
var sentimentResponse:NSDictionary! 

func getSentiment() { 
    var error:NSError? 
    var encodedText = textfield.text.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 


    Get.JSON(url: sentimentURL+encodedText!) { 
     (response) in 
     self.sentimentResponse = response 

     DispatchQueue.main.async() { 

      var agggregate = self.sentimentResponse["aggregate"]!["sentiment"]! as String 
      self.label.text = agggregate 

     } 
    } 
} 

在這一行 「變種agggregate = self.sentimentResponse [」彙總「]![」情「作爲字符串!」 即時得到錯誤,如類型'任何'沒有下標Members.please幫助我在提前排序此問題.thankyou ...得到錯誤 - 類型「任何」有沒有下會員 - swift3

+0

仍然沒有得到這個錯誤plz幫助排序這個錯誤@ericaya –

回答

0

您正在使用的功能Get.JSON。你不會告訴我們這是什麼,或者什麼數據類型的響應。

我猜測它是Any,或者[String:Any]

您的代碼self.sentimentResponse["aggregate"]!["sentiment"]!失敗,因爲編譯器不知道self.sentimentResponse包含的數據類型。

你可以嘗試強制轉換爲正確的類型:

guard let typedResponse = response as? [String: [String:String]] else {return} 
self.sentimentResponse = typedResponse 
guard let aggregate = typedResponse["aggregate"] else {return} 
guard let sentiment = aggregate["sentiment"] else {return} 
DispatchQueue.main.async() { 
    self.label.text = sentiment 
} 

(在我使用3個獨立的後衛語句使調試容易上面的代碼中你可以使用3條獨立的語句讓一個後衛聲明並完成相同的事情)

+0

hey @duncanc tanq回答時,我運行與我的代碼即時通訊獲取此錯誤的第一行代碼initilizer for conditionbinding必須有可選類型,而不是[字符串:[字符串:字符串]] –

+0

對不起。在第一行中,'as!'應該是'as'? –

+0

tanq非常適合現在它的工作... –

相關問題