-1
我想知道爲什麼不允許在AnyObject
Swift 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.2
AnyObject
到[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]
:
爲什麼下標不雨燕2.2 AnyObject
允許任何想法?
您正在使用哪些其他庫?是否有可能其他庫定義'[]'運算符?我無法在一個乾淨的項目上重現您的問題。 – Sulthan
我正在使用「解析」框架。 –
由於您知道更具體的'Dictionary'類型,您爲什麼使用不太特定的'AnyObject'? Swift的基本概念鼓勵開發者儘可能地使用類型。 – vadian