Reputation: 51
I am new to Swift and am having trouble retrieving information in a plist (see image), specifically in terms of iterating over the content and comparing the plist to user input. I have a
"Type 'String' does not conform to protocol 'Sequence Type'
error in my code.
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
}
}
}
}
}
Upvotes: 1
Views: 321
Reputation: 4219
You are telling Swift that locals
is a string. I think you meant to define it as [String]
.
if let locals = map["Local"] as? String {
Change to:
if let locals = map["Local"] as? [String] {
Your plist data does not match the code. What you have in the plist is a dictionary of array of String.
[String: [String]]
The code is attempting to access it as dictionary of array of dictionary of string: string.
[String: [String: String]]
You can either modify the plist data or change the code. Which one would you like?
Upvotes: 2
Reputation: 5850
You need to define the type you are expecting.
It looks like you're expecting locals
to be an array of dictionaries (either [String: String]
or [String: AnyObject]
, so try casting map["Local"]
to that (i.e. [[String: String]]
).
Ultimately a plist is a way to serialise structured data. You need to understand the structure you're dealing with before you can start working you way through one.
Upvotes: -1