Khoury Khoury - 4 months ago 10
iOS Question

What format should NSUserDefaults object be for Array index

I have a func to replace an item at a specific index number.

let editNumber = userDefault.objectForKey("editnumber") as! String
Globals.datesArray[editNumber] = dateString


However, I get an error


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


What format should the object from user defaults be in this scenario?

Answer

Just change:

let editNumber = userDefault.objectForKey("editnumber") as! String
Globals.datesArray[editNumber] = dateString

to

let editNumber = userDefault.objectForKey("editnumber") as! Int
Globals.datesArray[editNumber] = dateString //this is subscripting

You have to subscript an array with an Int.

Comments