简体   繁体   中英

Cannot subscript a value of type '[(String)]?' with an index of type 'Int'

I am trying to display a tableview of categories with each category having its own set of categories.

For example, the main categories are food, entertainment, recreation, and within the food section of the tableview, mexican food, asian food etc are displayed under that section.

However, I am running into this error:

Cannot subscript a value of type '[(String)]?' with an index of type 'Int'

Here's my code:

var categoryDictionary = [String:[String]]();
var categoriesList = ["Food", "Entertainment", "Recreation", "Shopping", "Transport", "Post Office"]
var foodCategories = [String]();
var entertainmentCategories = [String]();
var recreationCategories = [String]();
var shoppingCategories = [String]();
var transportCategories = [String]();
var lodgingCategories = [String]();
var librariesCategories = [String]();
var banksCategories = [String]();
var postOfficeCategories = [String]();

Here's just an example of appending to the array and adding the key value of "Food" to the array of foodCategory

func setupCategoryFood() {
    var asianFood = "Asian Food"
    var mexicanFood = "Mexican Food"
    var fastFood = "Fast Food"
    var MiddleEasternFood = "Middle Eastern Food"

    foodCategories.append(asianFood);
    foodCategories.append(mexicanFood);
    foodCategories.append(fastFood);
    foodCategories.append(MiddleEasternFood);

    categoryDictionary["Food"] = foodCategories;
}

and then...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell") as! UITableViewCell;

    var sectionTitle = categoriesList[indexPath.section]
    var sectionArray = categoryDictionary[sectionTitle];
    var itemInArray = sectionArray[indexPath.row];

    return cell
}

I get the error when I try to set a variable to equal the item inside of the array at the given indexpath.row:

'Cannot subscript a value of type '[(String)]?' with an index of type 'Int'

I'm really confused why this isn't working, so any help would be great.

Your sectionArray is optional so you can access it's object this way:

var itemInArray = sectionArray?[indexPath.row]

And your Output will be:

Optional("Asian Food")
Optional("Mexican Food")
Optional("Fast Food")

And if you don't want optional then you can unwrap it this way:

var itemInArray = sectionArray![indexPath.row]

And your Output will be:

Asian Food
Mexican Food
Fast Food

You get the error when trying to subscript an optional array. it would be best to check that the array is not nil before accessing it.

if let sectionArray = sectionArray {
    itemInArray = sectionArray[indexPath.row] 
}

or if it's ok with a nil value being set in itemInArray you could use

itemInArray = sectionArray?[indexPath.row] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM