我有2个自定义类。我正在尝试将likes
类的Like
字典值赋给likes
类的Post
属性值。但我明白了:
类型'赞'没有下标成员
class Like {
var likes: Dictionary<String, Any>?
var userUid: String?
}
extension Like {
static func transfromLikes(dictionary: [String : Any]) -> Like {
let like = Like()
like.likes = dictionary["likes"] as? Dictionary<String, Any>
like.userUid = dictionary["userUid"] as? String
return like
}
}
class Post {
var id : String?
var title : String?
var content : String?
var userUid : String?
var isLiked : Bool? = false
var likes : Like?
var likesCount : Int?
}
extension Post {
static func transformDataToImagePost (dictionary: [String : Any], key: String) -> Post {
let post = Post()
let like = Like()
post.id = key
post.userUid = dictionary["userUid"] as? String
post.title = dictionary["title"] as? String
post.likes = like.likes?["likes"] as? Like
if let currentUserId = Api.Users.CURRENT_USER?.uid {
if post.likesCount != nil {
post.isLiked = post.likes?[currentUserId] != nil // this is the error
}
}
return post
}
答案 0 :(得分:1)
post.likes
的类型为Like?
。您需要在下载之前将属性likes
添加到该属性:
post.isLiked = post.likes?.likes?[currentUserId] != nil