Type Any没有下标成员

时间:2017-08-30 03:13:18

标签: ios swift swift3 native

我发现很难将完成回调中的结果传递给我的ViewController中的访问权限。我可以在执行for循环时打印对象,但是我无法访问对象内的特定值。

public func getMedia(completion: @escaping (Array<Any>) -> ()){
    Alamofire.request(URL(string: MEDIA_URL)!,
        method: .get)
        .responseJSON(completionHandler: {(response) -> Void in
            if let value = response.result.value{
                let json = JSON(value).arrayValue
                completion(json)
            }
        }
    )
}

在我的ViewController中

    getMedia(){success in
        for item in success{
            print(item["image"]) //This causes error
            print(item) //This prints the object perfectly
        }
    }

1 个答案:

答案 0 :(得分:0)

问题正是错误消息告诉你的:要下标变量,需要将其声明为支持下标的类型,但是你已经将数组声明为包含AnyAny是最基本的类型,不支持下标。

假设您的数组包含字典,请改为声明getMedia函数:

public func getMedia(completion: @escaping ([[String : Any]]) -> ())

或者,如果字典可能包含非字符串键:

public func getMedia(completion: @escaping ([[AnyHashable : Any]]) -> ())

如果字典中的值都具有相同的类型,请根据需要将Any替换为该类型。

相关问题