下面是類的截圖,我得到了錯誤:斯威夫特的Xcode錯誤「無法下標類型‘[ListOfAnimals]’的值
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
}
}
編輯:當我使用「動物[indexPath.row]?‘我得到這個錯誤:’對非可選值不能使用可選的鏈接鍵入'[ListOfAnimals]'「.....但是當我使用」動物![indexPath.row]「時,我得到這個錯誤:」無法強制展開非可選類型的值[ListOfAnimals]'「.. ...但後來當我使用「動物[indexPath.row]」,我得到這個錯誤:「不能下標值類型'[ListOfAnimals]'」.....但後來我使用該行「var animal = Animal.self「,因爲我是偏執的,如果我沒有使用它,comp iler只是抱怨,並說動物沒有初始化或類似的東西
當我使用「動物[indexPath.row]?‘我得到這個錯誤:’對非可選值不能使用可選的鏈接的類型'[ListOfAnimals]'「 .....但是當我使用」動物![indexPath.row]「時,我得到這個錯誤:」不能強制unwrap非非可選類型的值'[ListOfAnimals]' 「.....但是然後 當我使用」動物[indexPath.row]「,我得到這個錯誤:」不能下標值類型'[ListOfAnimals]'「.....但是然後 我是usin g那行「var animal = Animal.self」,因爲我是偏執的,如果我不使用它,編譯器會抱怨並說動物沒有被初始化或類似的東西。 – David
我建議你按照我的建議刪除ListOfAnimals類,然後使用Animal。動物內部不需要'var animal',所以你應該刪除那條線。 – Michael
確定刪除該類後,我在同一行代碼中發現另一個錯誤,表示「Type'Animal.Type'沒有下標成員。」我如何爲它創建一個下標成員? – David