在使用NSSet构造可用的Swift数组时遇到麻烦



我正在使用Swift,使用字典,然后尝试将信息放入数组,并访问数组中的值。这就是我代码中的内容:

// 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

之后,在tableView函数中,我有以下代码:

println(countries[1])
cell.textLabel.text = countries[(indexPath.row)]

println工作正常(打印"Canada"),但是单元格。textLabel行给出以下错误(不会构建):

找不到接受所提供参数的'subscript'的重载

怎么打印不出下一行。我还应该提到,如果我只是引用一个我用简单的方式构建的数组,那么第二行就可以工作。我的问题是我创建"国家"数组的方式?还是我引用它的方式有问题?由于

这样做:

let countries = NSSet(array: Array<String>(plateIssuers.values)).allObjects as Array<String>
Swift的编译器像往常一样对这个问题的表达含混不清。这里的意思是,下标AnyObject的数组,这是从allObjects返回的,是可能的,但不会给你一个字符串,这是标签的text属性所期望的。

最新更新