“喜欢”类型没有下标成员?

时间:2018-06-16 15:31:59

标签: swift xcode class subscript

我有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
    }

1 个答案:

答案 0 :(得分:1)

post.likes的类型为Like?。您需要在下载之前将属性likes添加到该属性:

post.isLiked = post.likes?.likes?[currentUserId] != nil