【发布时间】:2014-09-23 12:23:42
【问题描述】:
我正在尝试在 Swift 中创建一个函数,它将字符串字典作为参数,并返回一个字符串元组。我希望字典中的键值对是可选的,因为如果它返回的元组中的值之一是“nil”,我不希望我的程序崩溃。
func songBreakdown(song songfacts: [String?: String?]) -> (String, String, String) {
return (songfacts["title"], songfacts["artist"], songfacts["album"])
}
if let song = songBreakdown(song: ["title": "The Miracle",
"artist": "U2",
"album": "Songs of Innocence"]) {
println(song);
}
第一行有一条错误消息:“Type 'String? does not conform to protocol 'Hashable'。
我尝试使参数的键值对不是可选的...
func songBreak(song songfacts: [String: String]) -> (String?, String?, String?) {
return (songfacts["title"], songfacts["artist"], songfacts["album"])
}
if let song = songBreak(song: ["title": "The Miracle", "artist": "U2", "album": "Songs of Innocence"]) {
println(song);
}
然后有一条错误消息说:“条件绑定中的绑定值必须是可选类型。我该如何解决这个问题?
【问题讨论】: