在下面的代码中,因为我在打印" aMovie.moviesGenere"时遇到错误。在操场上:
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]
答案 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)