我是Swift新手,無法檢索plist中的信息(請參閱圖像),特別是在迭代內容和比較plist與用戶輸入方面。我有一個Swift:使用for循環從plist中檢索數據
「類型 '串' 不符合協議在我的代碼 '序列類型'
錯誤。
func matchTraveller(location: Destination, preference: TravellingPreference) -> String{
var message = ""
if let path = NSBundle.mainBundle().pathForResource("Locals", ofType: "plist"){
if let map = NSDictionary(contentsOfFile: path){
if let locals = map["Local"] as? String {
for local in locals{ // <-- ERROR HERE
let city = local["City"]
if local["City"] == location {
message = "We have matched you with \(local) in \(city)."
} else{
message = "Apologies, there aren't any locals registered in \(city) on LocalRetreat. Try again soon!"
break
}
if local["Preference"] == preference{
let prefer = local["Preference"]
message += "\(local) is also a \(prefer)"
}
return message
}
}
}
}
}
不要將代碼發佈爲圖像,將實際文本放回原位,對於plist也是如此 – rmaddy
閱讀錯誤。 'locals'是一個'String',但你試圖像數組一樣迭代它。 – rmaddy