gpt4 book ai didi

ios - Swift xcode 错误 "cannot subscript a value of type ' [ListOfAnimals]'

转载 作者:行者123 更新时间:2023-11-30 13:40:14 24 4
gpt4 key购买 nike

这是我收到错误的类的屏幕截图:

http://i.stack.imgur.com/MOmlM.png

我不确定下标到底意味着什么,尽管一开始我认为它意味着它是一个类的现有成员。

这是第一个类

import UIKit
class AnimalListTableViewController: UITableViewController
{
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}

override func prepareForSegue(segue: UIStoryboardSegue,
sender: AnyObject?)
{
if let detailViewController = segue.destinationViewController as? DetailViewController, let indexPath = self.tableView.indexPathForSelectedRow {
detailViewController.animal = animals[indexPath.row] //This is where I get the error: Cannot subscript a value of type '[ListOfAnimals]'
}
}
}

这是 ListOfAnimals 类

import UIKit


let animals = [
ListOfAnimals(name: "Cow",
shortDescription: "Cattle",
longDescription: "A cow is a mature female and bull of an adult male of a bovine family. A heifer is a female cow that hasn't had a calf yet. Cattle is the name for the whole cow family. THere are about 920 different breeds of cows in the world."),

ListOfAnimals(name: "Bird",
shortDescription: "Usually small, has wings, feathers, and can fly.",
longDescription: "A warm-blooded egg-laying vertebrate distinguished by the possession of feathers, wings, and a beak and (typically) by being able to fly."),

ListOfAnimals(name: "Dolphin",
shortDescription: "A large fish",
longDescription: "A small gregarious toothed whale that typically has a beaklike snout and a curved fin on the back. Dolphins have become well known for their sociable nature and high intelligence."),

ListOfAnimals(name: "Dog",
shortDescription: "Man's best friend",
longDescription: "A domesticated carnivorous mammal that typically has a long snout, an acute sense of smell, and a barking, howling, or whining voice. It is widely kept as a pet or for work or field sports."),

ListOfAnimals(name: "Zebra",
shortDescription: "A horse with white and black stripes",
longDescription: "an African wild horse with black-and-white stripes and an erect mane."),

]

class ListOfAnimals
{


var name: String
//var type: Type
var shortDescription: String
var longDescription: String

init(name: String, shortDescription: String, longDescription: String)
{
self.name = name
self.shortDescription = shortDescription
self.longDescription = longDescription
}

}

这是动物类

import UIKit

class Animal
{
var animal = Animal.self
var name: String


var shortDescription: String
var longDescription: String
init(name: String, shortDescription: String, longDescription: String) {
self.name = name
self.shortDescription = shortDescription
self.longDescription = longDescription

}

}

编辑:当我使用“animals?[indexPath.row]”时,我收到此错误:“不能对类型“[ListOfAnimals]”的非可选值使用可选链接”......但是当我使用“animals![indexPath.row]”,我收到此错误:“无法强制解开非可选类型'[ListOfAnimals]'的值”...但是当我使用“animals[indexPath.row]”时,我收到此错误:“无法为 '[ListOfAnimals]' 类型的值添加下标”......但后来我使用了该行“var Animal = Animal.self”,因为我很偏执,如果我不使用它,编译器只会提示并说动物没有初始化或类似的东西

最佳答案

编辑:根据最新的代码更改,Animal 应该是 animalsAnimal 是类的名称,而您正在尝试为其中一种动物建立索引。

animals 只声明一次吗?错误消息表明,在您的第一个类中,animals 是可选的,在这种情况下,您必须在其后缀“?”或者 '!'。但是,您的 ListOfAnimals 类显示它是非可选的(如果这是相同的引用)。

 // If animals is non-Optional...
let animal = animals[indexPath.row]

// If animals is Optional
let animal = animals?[indexPath.row]

// If animals is Optional, but you know it's non-nil
let animal = animals![indexPath.row]

尽管如此,总的来说,尚不清楚 ListOfAnimals 存在的原因。看起来您想从 AnimalListTableViewController 中的一组动物中获取一种动物。因此我期望:

let animals = [Animal(...), Animal(...), Animal(...), Animal(...)]

还有您的 numberOfRowsInSection 返回 animals.count。上面的 let 将位于 AnimalListTableViewController 中,因此 ListOfAnimals 不需要存在。

也不清楚为什么需要以下行:

var animal = Animal.self

关于ios - Swift xcode 错误 "cannot subscript a value of type ' [ListOfAnimals]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35664211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com