无法下标“[(String)]”类型的值?索引类型为 'Int'
Cannot subscript a value of type '[(String)]?' with an index of type 'Int'
我正在尝试显示类别的表格视图,每个类别都有自己的一组类别。
例如,主要类别是食品、娱乐、休闲,在 tableview 的食品部分中,墨西哥食品、亚洲食品等显示在该部分下。
但是,我 运行 遇到了这个错误:
Cannot subscript a value of type '[(String)]?' with an index of type 'Int'
这是我的代码:
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]();
这里只是一个追加数组的例子,将"Food"的key值添加到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;
}
然后...
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
}
当我尝试将变量设置为等于给定 indexpath.row:
数组内的项目时出现错误
'Cannot subscript a value of type '[(String)]?' with an index of type 'Int'
我真的很困惑为什么这不起作用,所以任何帮助都会很棒。
您的 sectionArray
是可选的,因此您可以通过这种方式访问它的对象:
var itemInArray = sectionArray?[indexPath.row]
你的输出将是:
Optional("Asian Food")
Optional("Mexican Food")
Optional("Fast Food")
如果你不想要可选的,那么你可以这样解包:
var itemInArray = sectionArray![indexPath.row]
你的输出将是:
Asian Food
Mexican Food
Fast Food
尝试为可选数组下标时出现错误。最好在访问之前检查数组是否为 nil。
if let sectionArray = sectionArray {
itemInArray = sectionArray[indexPath.row]
}
或者如果可以在 itemInArray
中设置 nil 值,您可以使用
itemInArray = sectionArray?[indexPath.row]
我正在尝试显示类别的表格视图,每个类别都有自己的一组类别。
例如,主要类别是食品、娱乐、休闲,在 tableview 的食品部分中,墨西哥食品、亚洲食品等显示在该部分下。
但是,我 运行 遇到了这个错误:
Cannot subscript a value of type '[(String)]?' with an index of type 'Int'
这是我的代码:
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]();
这里只是一个追加数组的例子,将"Food"的key值添加到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;
}
然后...
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
}
当我尝试将变量设置为等于给定 indexpath.row:
数组内的项目时出现错误'Cannot subscript a value of type '[(String)]?' with an index of type 'Int'
我真的很困惑为什么这不起作用,所以任何帮助都会很棒。
您的 sectionArray
是可选的,因此您可以通过这种方式访问它的对象:
var itemInArray = sectionArray?[indexPath.row]
你的输出将是:
Optional("Asian Food")
Optional("Mexican Food")
Optional("Fast Food")
如果你不想要可选的,那么你可以这样解包:
var itemInArray = sectionArray![indexPath.row]
你的输出将是:
Asian Food
Mexican Food
Fast Food
尝试为可选数组下标时出现错误。最好在访问之前检查数组是否为 nil。
if let sectionArray = sectionArray {
itemInArray = sectionArray[indexPath.row]
}
或者如果可以在 itemInArray
中设置 nil 值,您可以使用
itemInArray = sectionArray?[indexPath.row]