2016-03-24 54 views
-1

我想知道爲什麼不允許在AnyObjectSwift 2.2(Xcode 7.3)突然顯示錯誤「'下標'」的模糊使用下標。爲什麼在Swift 2.2的AnyObject上不允許使用下標?

下面是我的代碼是工作的罰款之前:

func sampleMethod() { 
    PFCloud.callFunctionInBackground("sampleFunction", withParameters: nil) { (response, error) -> Void in 
     guard let response = response else { 
      if let error = error { 
       print(error) 
      } 
      return 
     } 

     if let records = response as? [AnyObject] { // Valid response 
      for record in records { 
       if let activeCount = record["activeCount"] as? Int { 
        print("activeCount: \(activeCount)") 
       } 

       if let persons = record["persons"] as? [AnyObject] { 
        for person in persons { 
         if let age = person["age"] as? Int { 
          print("age: \(age)") 
         } 
         if let properties = person["properties"] as? [AnyObject] { 
          for property in properties { 
           if let propertyName = property["name"] as? String { 
            print("propertyName: \(propertyName)") 
           } 
           if let propertyValue = property["value"] as? String { 
            print("propertyValue: \(propertyValue)") 
           } 
          } 
         } 
        } 
       } 
      } 
     } else { 
      print("Invalid response") 
     } 
    } 
} 

這裏是我的代碼改變後,也就是現在的工作在Swift 2.2AnyObject[String: AnyObject]

func sampleMethod() { 
    PFCloud.callFunctionInBackground("sampleFunction", withParameters: nil) { (response, error) -> Void in 
     guard let response = response else { 
      if let error = error { 
       print(error) 
      } 
      return 
     } 

     if let records = response as? [[String: AnyObject]] { // Valid response 
      for record in records { 
       if let activeCount = record["activeCount"] as? Int { 
        print("activeCount: \(activeCount)") 
       } 

       if let persons = record["persons"] as? [[String: AnyObject]] { 
        for person in persons { 
         if let age = person["age"] as? Int { 
          print("age: \(age)") 
         } 
         if let properties = person["properties"] as? [[String: AnyObject]] { 
          for property in properties { 
           if let propertyName = property["name"] as? String { 
            print("propertyName: \(propertyName)") 
           } 
           if let propertyValue = property["value"] as? String { 
            print("propertyValue: \(propertyValue)") 
           } 
          } 
         } 
        } 
       } 
      } 
     } else { 
      print("Invalid response") 
     } 
    } 
} 

下面是我在解決一系列錯誤時的屏幕截圖中號改變AnyObject[String: AnyObject]

enter image description here


enter image description here


enter image description here

爲什麼下標不雨燕2.2 AnyObject允許任何想法?

+1

您正在使用哪些其他庫?是否有可能其他庫定義'[]'運算符?我無法在一個乾淨的項目上重現您的問題。 – Sulthan

+0

我正在使用「解析」框架。 –

+0

由於您知道更具體的'Dictionary'類型,您爲什麼使用不太特定的'AnyObject'? Swift的基本概念鼓勵開發者儘可能地使用類型。 – vadian

回答

1

首先,我不能重現這個問題。這將編譯並在Xcode 7.3(2.2雨燕)運行對我罰款:

var records = [AnyObject]() 
records.append(["howdy":"hello"]) 
for record in records { 
    if let x = record["howdy"] { 
     print(x) 
    } 
} 

我會因此有猜測,你所看到的問題是,你使用的是一些庫,如解析介紹。大部分還有一些其他的下標定義(可能是objectForKeyedSubscript:)。

但第二,你應該從來沒有這樣做過。 Swift是關於嚴格打字的。下載AnyObject是愚蠢的,就像向AnyObject發送任何消息一樣。即使你能擺脫它,你也不應該這樣做。有一些明智的方法可以確保你得到的是一本字典,並在之前投下你的下標字典。使用它們。

相關問題