简体   繁体   English

无法为索引类型为“ String!”的“ [Int:[String]]”类型下标!

[英]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.

相关问题 不能使用类型为“String?”的索引对类型为“[String : Int]”的值进行下标 - Cannot subscript a value of type '[String : Int]' with an index of type 'String?' 无法使用“Int”类型的索引下标“[String]”类型的值 - Cannot subscript a value of type '[String]' with an index of type 'Int' 无法下标'[(String)]类型的值?'索引类型为'Int' - Cannot subscript a value of type '[(String)]?' with an index of type 'Int' 不能为“Set”类型的值添加下标<String> &#39; 带有类型为 &#39;Int&#39; 的索引 - Cannot subscript a value of type 'Set<String>' with an index of type 'Int' 不能使用索引类型(String,Int)Swift 2下标类型[(String,Int)]的值 - Cannot subscript a value of type [(String, Int)] with an index of type (String, Int) Swift 2 无法下标[AnyObject]的值? 索引类型为String - Cannot subscript a value of [AnyObject]? with an index of type String 无法用索引类型“ String”下标“ [[[String:Any]]””的值 - Cannot subscript a value of type '[[String : Any]]' with an index of type 'String' 无法用索引类型“ String”下标“ String”类型的值 - Cannot subscript a value of type 'String' with an index of type 'String' 不能下标类型为[String:AnyObject]的值?索引类型为String - Cannot subscript a value of type [String: AnyObject]? with an index of type String 无法使用索引类型为“String”的类型&#39;[String:AnyObject]&#39;下标值 - Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM