[英]How do I use Optional Chaining with nil coalescing operator?
Using the enums and structures, implement the getArtistGenre() function.使用枚举和结构,实现 getArtistGenre() function。 Given a Song as input, return the raw value of the artist's primary genre.
给定一首歌曲作为输入,返回艺术家主要流派的原始值。 If either the artist is nil or the primaryGenre is nil, then return an empty string.
如果艺术家为 nil 或 primaryGenre 为 nil,则返回一个空字符串。 You must use optional chaining and the nil coalescing operator (??).
您必须使用可选链接和 nil 合并运算符 (??)。
enum Genre: String {
case country, blues, folk
}
struct Artist {
let name: String
var primaryGenre: Genre?
}
struct Song {
let title: String
let released: Int
var artist: Artist?
}
func getArtistGenre(song: Song) -> String {
}
As the comments suggest:正如评论所暗示的:
func getArtistGenre(song: Song) -> String {
return song.artist?.primaryGenre?.rawValue ?? “”
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.