打印变量时出错lldb_expr

时间:2018-02-21 05:35:44

标签: arrays swift enums swift-playground

在下面的代码中,因为我在打印" aMovie.movi​​esGenere"时遇到错误。在操场上:

struct Movie: Codable {
    enum MovieGenere: String, Codable {
        case horror, skifi, comedy, adventure, animation
    }

    var name : String
    var moviesGenere : [MovieGenere]
    var rating : Int
}

let aMovie = Movie(name: "Up", moviesGenere: [.comedy , .adventure, .animation], rating : 4)

print(aMovie.moviesGenere)

错误:

[__lldb_expr_98.Movie.MovieGenere.comedy, __lldb_expr_98.Movie.MovieGenere.adventure, __lldb_expr_98.Movie.MovieGenere.animation]

2 个答案:

答案 0 :(得分:0)

错误,低级调试器以及Playground如何打印出您的枚举。如果你将其分解,你会看到:

[
 __lldb_expr_98。的 Movie.MovieGenere.comedy 下,
__lldb_expr_98。 Movie.MovieGenere.adventure
  __lldb_expr_98。的 Movie.MovieGenere.animation
]

答案 1 :(得分:0)

如果要从控制台输出中删除__lldb_expr前缀。那么struct必须符合CustomStringConvertible协议。这是一个公共协议,它具有一个var描述,我们可以在其中提供自定义字符串以显示结构描述。

struct Movie: Codable, CustomStringConvertible {
    enum MovieGenere: String, Codable, CustomStringConvertible {
        var description: String {
            return self.rawValue
        }

        case horror, skifi, comedy, adventure, animation
    }

    var name : String
    var moviesGenere : [MovieGenere]
    var rating : Int

    var description: String {
        return "\(name), \(moviesGenere), \(rating)"
    }
}

let aMovie = Movie(name: "Up", moviesGenere: [.comedy , .adventure, .animation], rating : 4)
print(aMovie)