[英]Cannot subscript a value of type '[Int : [String]]' with an index of type 'String!'
please, ask me, where is my mistake? 请问我,我的错误在哪里? I have Xcode error:
我有Xcode错误:
Cannot subscript a value of type '[Int : [String]]' with an index of type 'String!'
无法为索引类型为“ String!”的“ [Int:[String]]”类型下标!
in let keyExists = myDict[tmp.Hour] != nil, myDict[tmp.Hour] = Int and myDict[tmp.Hour].append(tmp.Minutes) of that part of code: 在let keyExists = myDict [tmp.Hour]!= nil中,myDict [tmp.Hour] = Int和myDict [tmp.Hour] .append(tmp.Minutes):
func array() -> Dictionary <Int,[String]>
{
let timeInfos = getTimeForEachBusStop()
var myDict: Dictionary = [Int:[String]]()
for tmp in timeInfos {
let keyExists = myDict[tmp.Hour] != nil
if (!keyExists) {
myDict[tmp.Hour] = [Int]()
}
myDict[tmp.Hour].append(tmp.Minutes)
}
return myDict
}
I understand, that problem is in optional type, but where is problem I don't understand 我了解,该问题属于可选类型,但我不了解的问题在哪里?
upd 更新
func getTimeForEachBusStop() -> NSMutableArray {
sharedInstance.database!.open()
let lineId = getIdRoute
let position = getSelectedBusStop.row + 1
let getTimeBusStop: FMResultSet! = sharedInstance.database!.executeQuery("SELECT one.hour, one.minute FROM shedule AS one JOIN routetobusstop AS two ON one.busStop_id = (SELECT two.busStop_id WHERE two.line_id = ? AND two.position = ?) AND one.day = 1 AND one.line_id = ? ORDER BY one.position ASC ", withArgumentsInArray: [lineId, position, lineId])
let getBusStopInfo : NSMutableArray = NSMutableArray()
while getTimeBusStop.next() {
let stopInfo: TimeInfo = TimeInfo()
stopInfo.Hour = getTimeBusStop.stringForColumnIndex(0)
stopInfo.Minutes = getTimeBusStop.stringForColumnIndex(1)
getBusStopInfo.addObject(stopInfo)
}
sharedInstance.database!.close()
return getBusStopInfo
}
You are declaring your dictionary as a dictionary with keys of type Int
and values of type [String]
: 您正在将字典声明为具有
Int
类型的键和[String]
类型的值的字典:
var myDict: Dictionary = [Int:[String]]()
(better written as: var myDict: [Int: [String]] = [:]
because by casting it to Dictionary
you are removing the types). (更好地写为:
var myDict: [Int: [String]] = [:]
因为将其强制转换为Dictionary
即可删除类型)。
However, in 但是,在
myDict[tmp.Hour] = [Int]()
You are using a value which is of [Int]
type and tmp.Hour
is probably a String
. 您正在使用
[Int]
类型的值,并且tmp.Hour
可能是String
。
So, your problem is a type mismatch. 因此,您的问题是类型不匹配。
The error states that you cannot subscribe your [Int:[String]]
dictionary with a String
key. 该错误表明您无法使用
String
键预订[Int:[String]]
字典。
Therefore the type of tmp.Hour
is obviously String
rather than the expected Int
因此,
tmp.Hour
的类型显然是String
而不是预期的Int
If tmp.Hour
is guaranteed to be an integer string you can convert the value 如果保证
tmp.Hour
为整数字符串,则可以将值转换为
let hour = Int(tmp.Hour)!
myDict[hour] = [Int]()
On the other hand since myDict
is [Int:[String]]
you might mean 另一方面,由于
myDict
为[Int:[String]]
您可能会说
let hour = Int(tmp.Hour)!
myDict[hour] = [String]()
Hour and Minutes are of type string
(I guess - stringForColumnIndex
) so your dictionary is wrong type. Hour和Minutes是
string
类型(我猜是stringForColumnIndex
),因此您的字典类型错误。 Should be: 应该:
func array() -> Dictionary <String,[String]>
{
let timeInfos = getTimeForEachBusStop()
var myDict: Dictionary = [String:[String]]()
for tmp in timeInfos {
let keyExists = myDict[tmp.Hour] != nil
if (!keyExists) {
myDict[tmp.Hour] = [String]()
}
myDict[tmp.Hour].append(tmp.Minutes)
}
return myDict
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.