【问题标题】:Error lldb_expr when printing a variable打印变量时出错 lldb_expr
【发布时间】:2018-02-21 05:35:43
【问题描述】:

在以下代码中,因为在 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]

【问题讨论】:

    标签: arrays swift enums swift-playground


    【解决方案1】:

    不是错误,它是 Low Level Debugger 以及 Playground 如何打印出您的枚举。如果你分解它,你会看到:

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

    【讨论】:

    • 感谢您的回复,以便能够访问我可以使用 For-in 的值
    • @MarioBurga,你有那里的值,但如果你使用这个for movie in aMovie.moviesGenere { print(movie) },你会打印出//comedy //adventure //animation
    【解决方案2】:

    如果您想从控制台输出中删除 __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)
    

    【讨论】:

      猜你喜欢
      • 2018-09-28
      • 2020-02-11
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      相关资源
      最近更新 更多