我想知道为什么突然不允许下标AnyObject
并显示错误“模糊使用'下标' ”。Swift 2.2(Xcode 7.3)
以下是我以前运行良好的代码:
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]
:
AnyObject
关于为什么Swift 2.2 中不允许下标的任何想法?