gpt4 book ai didi

arrays - 使用 NSSet 构建可用的 Swift 数组时遇到问题

转载 作者:可可西里 更新时间:2023-11-01 01:06:50 25 4
gpt4 key购买 nike

我正在使用 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 工作正常(打印“加拿大”),但 cell.textLabel 行给出以下错误(不会构建):

Could not find an overload for 'subscript' that accepts the supplied arguments

println怎么能工作却下一行不行呢。我还应该提到,如果我只是引用一个我以简单方式构建的数组,其中包含字符串,那么第二行是有效的。我的问题是我创建“国家”数组的方式吗?还是我引用它的方式有问题?谢谢

最佳答案

这样做:

let countries = NSSet(array: Array<String>(plateIssuers.values)).allObjects as Array<String>

像往常一样,Swift 的编译器在表达问题时含糊其辞。这里的意思是,下标 AnyObject 的数组,这是从 allObjects 返回的,是可能的,但不会给你一个字符串,这是标签的 text 属性期望。

关于arrays - 使用 NSSet 构建可用的 Swift 数组时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com