类型' Bool'没有下标成员

时间:2016-09-05 08:01:46

标签: ios swift

我是Swift的新手,并按照Appcoda的书来研究IOS编程,这是我的代码: -In restaurantDetailViewController类:

class RestaurantDetailViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet var restaurantImageView:UIImageView!
@IBOutlet var restaurantNameLabel:UILabel!
@IBOutlet var restaurantTypeLabel:UILabel!
@IBOutlet var restaurantLocationLabel:UILabel!

var restaurantImage = ""
var restaurantName = ""
var restaurantType = ""
var restaurantLocation = ""
var isVisited:Bool?

 func tableView(tableView:UITableView,numberOfRowsInSection section:Int)->Int{
    return 4
}
func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell{
    let cellIndentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as!RestaurantDetailViewCell
    switch indexPath.row{
    case 0:
        cell.filedLabel.text = "Name"
        cell.valueLabel.text = restaurantName
    case 1:
        cell.filedLabel.text = "Type"
        cell.valueLabel.text = restaurantType
    case 2:
        cell.filedLabel.text = "Location"
        cell.valueLabel.text = restaurantLocation
    case 3:
        cell.filedLabel.text = "Been here?"
        cell.valueLabel.text = isVisited[indexPath.row] ? "Yes, I have been here" : "No"//I got the error right here

    default:
        cell.filedLabel = nil
        cell.valueLabel = nil

    }
    return cell
}
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    restaurantImageView.image = UIImage(named: restaurantImage)
    //restaurantNameLabel.text = restaurantName
    //restaurantTypeLabel.text = restaurantType
    //restaurantLocationLabel.text = restaurantLocation
}

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


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

我搜索了许多关于我的问题的话题,但是我还没有任何线索,如果你们能帮助我,我将非常感激。这是我第一次将我的问题发布到stackoverflow,所以如果我的主题有问题,我问这个问题,我希望你们可以温柔地提醒我。 非常感谢你。

1 个答案:

答案 0 :(得分:0)

iSVisited被声明为可选的布尔值

var isVisited:Bool?

更新了您的case3,如下所示: -

cell.valueLabel.text = isVisited ? "Yes, I have been here" : "No"