-1

我想知道为什么突然不允许下标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 中不允许下标的任何想法?

4

1 回答 1

1

首先,我无法重现该问题。这在 Xcode 7.3 (Swift 2.2) 中对我来说编译和运行良好:

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

因此,我不得不猜测您看到的问题是由您正在使用的某个库引入的,例如 Parse。大多数存在一些其他的下标定义(可能是objectForKeyedSubscript:)。

但其次,你一开始就不应该这样做。Swift 是关于严格类型的。下标 AnyObject 是愚蠢的,就像向 AnyObject 发送任何消息一样。即使你能逃脱惩罚,你也不应该这样做。有一些明智的方法可以确保您得到的是字典,并在下标之前转换为字典。使用它们。

于 2016-03-24T14:53:27.633 回答