2017-02-13 48 views
0

如果服務器響應是(結果=「帳戶存在」),那麼我可以做一些事情。但是當我比較結果時會給出一個錯誤,即「Binary operator'=='不能應用於'Any'和'String'類型的操作數」。如果條件在快速響應ios

let dataTask = session.dataTask(with: url!, completionHandler: { (data, response, error) -> Void in 

     do 
     { 
      let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary 

      print("JSON : \(json)") 

      if let dataDic = json.value(forKey: "data") 
      { 
      print("dataDic: \(dataDic)") 

      if let Result = (dataDic as AnyObject).value(forKey: "Result") 
      { 
      print("Result: \(Result)") 

       if (Result ("Account Exists")) == 0 
       { 
        //... DO Something 
       } 
      } 
      } 
     } 
     catch 
     { 
      print("Catch exception") 
     } 
    }) 
    dataTask.resume() 
} 
+0

我得到了「無法將類型'__NSSingleObjectArrayI'(0x10d935d60)的值轉換爲'NSString'(0x10cb46c40)」@ EICaptainv2.0 –

+0

您的迴應如何? –

+0

這是JSON數據=( 結果=「未經授權的訪問」; } ); } –

回答

0

東西在純快捷方式..你的「數據」是一個數組,頂級JSON是字典

do 
    { 
     let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any] 

     if let data = json["data"] as? [[String:String]] { 
      if let resultString = data.first?["Result"] as? String{ 
       if resultString == "Account Exists"{ 
        //... DO Something 
       } 
       else if resultString == "Unauthorized Access" { 
        //... DO Something 
       } 
      } 
     } 
    } 
    catch{ 
     print("Catch exception") 
    } 
+0

它有效。謝謝@ EICaptainv2.0 –

+0

很高興它幫助..快樂2幫助你:) –

0

請在下面更新您的代碼,並檢查它

if (Result ("Account Exists") as! String) == "0" 
{ 
     //... DO Something 
} 
+0

我收到此錯誤「無法調用非函數類型的值'字符串'」@Sunil prajapati –

+0

請參閱我更新了我的答案 –

0

如果您Result是一個字符串,你需要相應地拆開包裝,然後進行比較,可以實現比較而展開本身,你可以像

if let Result = (dataDic as AnyObject).value(forKey: "Result") as? String, Result == "Unauthorized Access" 
{ 
    print("Result: \(Result)") 
    // DO whatever you want here in case of Unauthorised access 
}