Vadim Nikolaev
Vadim Nikolaev

Reputation: 2132

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:

Cannot subscript a value of type '[Int : [String]]' with an index of type 'String!'

in let keyExists = myDict[tmp.Hour] != nil, myDict[tmp.Hour] = Int and myDict[tmp.Hour].append(tmp.Minutes) of that part of code:

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

    }

Upvotes: 0

Views: 2059

Answers (3)

vadian
vadian

Reputation: 285059

The error states that you cannot subscribe your [Int:[String]] dictionary with a String key.

Therefore the type of tmp.Hour is obviously String rather than the expected Int

If tmp.Hour is guaranteed to be an integer string you can convert the value

let hour = Int(tmp.Hour)!
myDict[hour] = [Int]()

On the other hand since myDict is [Int:[String]] you might mean

let hour = Int(tmp.Hour)!
myDict[hour] = [String]()

Upvotes: 0

Sulthan
Sulthan

Reputation: 130082

You are declaring your dictionary as a dictionary with keys of type Int and values of type [String]:

var myDict: Dictionary = [Int:[String]]()

(better written as: var myDict: [Int: [String]] = [:] because by casting it to Dictionary you are removing the types).

However, in

myDict[tmp.Hour] = [Int]()

You are using a value which is of [Int] type and tmp.Hour is probably a String.

So, your problem is a type mismatch.

Upvotes: 1

Tomasz Pikć
Tomasz Pikć

Reputation: 763

Hour and Minutes are of type string (I guess - stringForColumnIndex) so your dictionary is wrong type. 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
}

Upvotes: 0

Related Questions