I am working with Swift, using a dictionary, and then trying to put info into an array, and access values in the array. This is what I have in my code:
// I have abbreviated the actual dictionary to not waste question space...
var plateIssuers: Dictionary<String,String> = [
"Alberta": "Canada",
"British Columbia": "Canada",
"Manitoba":"Canada",
"VERMONT":"USA",
"WASHINGTON":"USA",
"WISCONSIN":"USA",
"WEST VIRGINIA":"USA",
"WYOMING]":"USA"]
// This is an attempt that seems to work to create an Array of unique values (turning into sets and then back to array
let countries:Array = NSSet(array: Array<String>(plateIssuers.values)).allObjects
let issuers:Array = NSSet(array: Array<String>(plateIssuers.keys)).allObjects
Later, in a tableView function, I have the following code:
println(countries[1])
cell.textLabel.text = countries[(indexPath.row)]
The println works fine (prints "Canada"), but the cell.textLabel line gives the following error (won't build):
Could not find an overload for 'subscript' that accepts the supplied arguments
How can the println work but not the next line. I should also mention that the second line works if I just refer to an array I built in a simple manner with strings in it. Is my problem the way I created the "countries" array? or is it a problem in how I am referencing it? Thanks
countries[indexPath.row]countriestoo loosely. You must type it asArray<String>: in particular, you must cast the result ofallObjectstoArray<String>. Every type in Swift must be extremely specific, and things are not magically downcast for you as they come across the bridge from Objective-C.