我正在尝试使用Snapchat的登录套件来允许用户使用Snapchat登录,但是他们的文档给我一个错误。
复制文档会给我一个“类型'[AnyHashable:任何]?”。没有下标成员。错误发生在“ let data =”行。如果我强制拆开资源,则错误将移至下一行
let graphQLQuery = "{me{displayName, bitmoji{avatar}, externalId}}"
let variables = ["page": "bitmoji"]
SCSDKLoginClient.fetchUserData(withQuery: graphQLQuery, variables: variables, success: { (resources: [AnyHashable: Any]?) in
let data = resources["data"] //error here
let me = data["me"]
let displayName = me["displayName"]
let bitmoji = me["bitmoji"]
let bitmojiAvatarUrl = bitmoji["avatar"]
}, failure: { (error: Error?, isUserLoggedOut: Bool) in
// handle error
})
如果我解包数据资源和数据,那么错误将移至我尝试设置me = data [“ me”]的行(错误=“ Type Any没有下标成员”)。然后,如果我尝试拆开包装,错误仍然存在。
例如:
let graphQLQuery = "{me{displayName, bitmoji{avatar}, externalId}}"
let variables = ["page": "bitmoji"]
SCSDKLoginClient.fetchUserData(withQuery: graphQLQuery, variables: variables, success: { (resources: [AnyHashable: Any]?) in
guard let response = resources else { return }
guard let data = response["data"] else { return }
print(data)
let me = data["me"] // error here
let displayName = me["displayName"]
let bitmoji = me["bitmoji"]
let bitmojiAvatarUrl = bitmoji["avatar"]
}, failure: { (error: Error?, isUserLoggedOut: Bool) in
// handle error
})
打印出数据返回:
{
me = {
bitmoji = {
avatar = "{URL}";
};
displayName = "{NAME}";
externalId = "{ID}";
};
}
答案 0 :(得分:0)
您必须告诉编译器静态集合类型,因为编译器希望证明其订阅能力。
if let data = resources["data"] as? [String:Any],
let me = data["me"] as? [String:Any] {
let displayName = me["displayName"] as! String
if let bitmoji = me["bitmoji"] as? [String:Any] {
let bitmojiAvatarUrl = bitmoji["avatar"] as! String
}
}