我是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