Here is a screenshot of the class where I got the error:
http://i.stack.imgur.com/MOmlM.png
I'm not sure exactly what a subscript means even though at first I thought it meant that it was an existing member of a class.
Here is the first class
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]'
}
}
}
Here is the ListOfAnimals class
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
}
}
Here is the Animal Class
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
}
}
EDIT: When I use “animals?[indexPath.row]", I get this error: "Cannot use optional chaining on non-optional value of type '[ListOfAnimals]'” ..... but then When I use "animals![indexPath.row]”, I get this error: “Cannot force unwrap value of non-optional type '[ListOfAnimals]'”.....but then When I use "animals[indexPath.row]”, I get this error: “Cannot Subscript a value of type '[ListOfAnimals]'”.....but then I was using that line "var animal = Animal.self" because I was paranoid that if I didn't use it, the compiler would just complain and say animal wasn't initialized or something like that
EDIT: With your latest code changes, Animal
should be animals
. Animal
is the name of the class, whereas you are trying to index one of the animals.
Is animals
only declared once? The error message suggests that in your first class, animals
is Optional, in which case you would have to suffix it with '?' or '!'. However, your ListOfAnimals class shows it is non-Optional, if that's the same reference.
// 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]
Nevertheless, on a general note, it's not clear why ListOfAnimals exists. It looks like you want to get one animal in AnimalListTableViewController from an array of animals. Therefore I would expect:
let animals = [Animal(...), Animal(...), Animal(...), Animal(...)]
And also your numberOfRowsInSection
to return animals.count
. The above let
would then be in AnimalListTableViewController
, and ListOfAnimals
therefore doesn't need to exist.
It is also not clear why you need the following line:
var animal = Animal.self
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.