2016-02-26 46 views
2

我想用動物列表做一個swift項目,然後當你點擊其中一行時,你會被運送到另一個窗口它告訴你動物的描述。Swift xcode錯誤:類型'DetailViewController'的值沒有成員'Animal'

這裏是代碼,如果截圖太小:

import UIKit 


class DetailViewController: UIViewController 
{ 
    @IBOutlet weak var titleLabel: UILabel! 
    @IBOutlet weak var descriptionLabel: UILabel! 
    var animal: Animal? 
    var a = Animal?() 


    override func viewWillAppear(animated: Bool) 
    { 
     titleLabel.text = a!.name 
     descriptionLabel.text = a!.longDescription 
    } 



    override func viewDidLoad() 
    { 
     viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() 
    { 
     didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 



} 

ListOfAnimals類代碼:

import Foundation 


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 
    } 

} 

回答

2

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 that says "Value of type 'DetailViewController' has no member 'Animal" 
     } 
    } 
} 

代碼DetailViewController類

用途:

detailViewController.animal 

相反的:

detailViewController.Animal 
+0

是工作,但現在我得到一個跟蹤誤差強調了「動物[indexPath.row]」和錯誤說「不能標a類型爲'[ListOfAnimals]'的值' – David

+0

如果它有效,請接受答案並創建一個新問題來描述您遇到的新問題並提供更多信息。 – RaffAl

相關問題