使用未声明类型的播放器'

时间:2016-10-04 06:04:59

标签: ios swift xcode

我在变量播放器中出错,我发现错误无法完成

import UIKit

class PlayersViewController: UITableViewController {

var players:[Player] = playersData

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return players.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
    -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("PlayerCell", forIndexPath: indexPath)

        let player = players[indexPath.row] as Player
        cell.textLabel?.text = player.name
        cell.detailTextLabel?.text = player.game
        return cell
    }

}

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要先定义Player类型。你可能会想到这样的事情:

class PlayersViewController: UITableViewController {

   var players: [Player] = []
   var xplayers: [XPlayer] = []

   // struct type of Player
   struct Player {
       var name : String = ""
       var height : Int = 0
   }

   // OR

   // tuple type of XPlayer
   typealias XPlayer = (String, Int)    

   func someMethod() {

       self.players.append(Player(name: "John Herbert", height: 160))

       // OR

       self.xplayers.append(("John Herbert", 160))

   }
}