我正在使用Swift,使用字典,然後嘗試將信息放入數組,並訪問數組中的值。這是我在我的代碼:使用NSSet構建可用的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的正常工作(打印「加拿大」),但細胞.textLabel線提供了以下錯誤(將不建立):
找不到接受提供的參數「下標」過載
println如何工作,但不是下一行。我還應該提到,如果我只是簡單地用一個字符串構建一個數組,我會提到第二行。我的問題是我創建「國家」數組的方式嗎?或者它是如何引用它的問題?由於
嘗試刪除括號。即'國家[indexPath.row]' –
謝謝,但這就是我原來的。我把它們放進來看看它是否會有所幫助,但事實並非如此。 – Narwhal
問題是你輸入'國家'太鬆散。您必須將其輸入爲'Array':特別是,您必須將'allObjects'的結果投射到'Array '。 Swift中的每個類型都必須非常具體,並且在Objective-C遇到橋時,對於您來說事情並不是神奇的。 –
matt