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

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

07-26 09:38