Как преобразовать словарь в пользовательский тип в Swift? - PullRequest
0 голосов
/ 20 ноября 2018

Я пытаюсь преобразовать этот словарь в пользовательский класс, но получаю SIGABRT на let comment = aComment as! messageComments, как я могу преобразовать словарь в пользовательский класс messageComments

["comment1": {
    color = grape;
    content = "a comment ";
    date = 563954564;
    "icon " = referee;
    userName = "anotherUser ";
}, "comment2": {
    color = grape;
    content = "another comment ";
    date = 563954564;
    icon = referee;
    userName = "user";
}]



let comments = messages.childSnapshot(forPath: "comments").value as?[String: Any] ?? [:]

            for aComment in comments.values {
                let comment = aComment as! messageComments
                let theComment = messageComments(content: comment.content, color: comment.color, icon: comment.icon, date: comment.date, userName: comment.userName)
                commentArray.append(theComment)
            }

1 Ответ

0 голосов
/ 21 ноября 2018

Это было решение

let comments = messages.childSnapshot(forPath: "comments").value as? [String: Any] ?? [:]



                for comment in comments {

                    let theComment = comment.value as? [String: Any]

                    let theContent = theComment?["content"] as? String ?? ""
                    let theIcon = theComment?["icon"] as? String ?? ""
                    let theColor = theComment?["color"] as? String ?? ""
                    let theDate = theComment?["date"] as? String ?? ""
                    let theName = theComment?["userName"] as? String ?? ""

                    let aComment = messageComments(content: theContent, color: theColor, icon: theIcon, date: theDate, userName: theName)
                    commentArray.append(aComment)

                }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...