I have 2 custom classes. And I'm trying to assign the value of likes
dictionary from Like
class to the value of likes
property of the Post
class. But I get the:
Type 'Like' has no subscript member
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
}
post.likes
is of type Like?
. You need to add the property likes
to that before subscripting it:
post.isLiked = post.likes?.likes?[currentUserId] != nil