简体   繁体   English

无法下标'[(String)]类型的值?'索引类型为'Int'

[英]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. 我正在尝试显示类别的tableview,每个类别都有自己的类别集。

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)]?' 无法下标'[(String)]类型的值?' with an index of type 'Int' 索引类型为'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 这里只是一个附加到数组并将“Food”的键值添加到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: 当我尝试将变量设置为等于给定indexpath.row中数组内部的项时,我收到错误

'Cannot subscript a value of type '[(String)]?' '不能下标'[(String)]类型的值?' with an index of type 'Int' 索引类型为'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: 您的sectionArray是可选的,因此您可以通过以下方式访问它的对象:

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. 最好在访问之前检查数组是否为nil。

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中设置了nil值, itemInArray可以使用

itemInArray = sectionArray?[indexPath.row] 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法为索引类型为“ String!”的“ [Int:[String]]”类型下标! - Cannot subscript a value of type '[Int : [String]]' with an index of type 'String!' 不能使用类型为“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' 不能为“Set”类型的值添加下标<String> &#39; 带有类型为 &#39;Int&#39; 的索引 - Cannot subscript a value of type 'Set<String>' with an index of type 'Int' 无法为索引为[[)]的类型为[Int]的值下标 - Cannot subscript a value of type '[Int]' with an index of type '()' 不能下标[AnyObject]的值? 索引类型为Int - Cannot subscript a value of [AnyObject]? 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 无法使用索引“ NSNumber”的类型对“ [Int:UIColor]”的值下标 - Cannot subscript a value of type '[Int : UIColor]' with an index of type 'NSNumber' 无法用索引类型Int下标字典类型的值 - Cannot subscript a value of type dictionary with an index of type Int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM