[问题] 练习实作时遇到的程式请教

楼主: MIZUYAMA (致命祈愿)   2015-07-21 12:57:44
在自学练习时依照课本上的程式码
但是Xcode却出现了错误
想请教板上高手给予指导
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath:
NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,
forIndexPath: indexPath) as UITableViewCell
^^^
// Configure the cell...
cell.textLabel?.text = restaurantNames[indexPath.row]
cell.imageView?.image = UIImage(named: "restaurant")
return cell
}
Xcode建议我是不是要用as!
但是as!用了后没出现问题但跑不出与范本一样的结果...
想请高手给予一点指导>"<
作者: stweber2001 (Weber)   2015-07-21 12:59:00
tableView.dequeueReusableCellWithIdentifier有可能回传nil, 如果用as!程式就会死给你看
楼主: MIZUYAMA (致命祈愿)   2015-07-21 13:01:00
范本用as 但是实作时出现错误 检查多次实在不知道哪里错
作者: stweber2001 (Weber)   2015-07-21 13:56:00
你把错误贴上来可能会比较清楚一点
作者: gradyzhuo   2015-07-21 15:36:00
dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) 一定会有cell, 所以as! 没问题,除非没register reuse identifier 不过满好奇是指怎样的与预期不同?
楼主: MIZUYAMA (致命祈愿)   2015-07-21 21:48:00
这就是错误的程式码= =Xcode是说as那出问题,执行后会说建立失败......as!可以但是会跟范例结果不同
作者: gradyzhuo   2015-07-22 01:31:00
我是问跟范例结果不同是指什么意思?因为as!是Swift 1.2 中改变的部分,在1.1之前是使用as 1.2之后改用as! 但跟程式运行没有关系,也就是as!就是以前as,所以执行起来是会一样的,除非你其他部分写的跟范本不同。所以还是有Code会比较清楚。

Links booklink

Contact Us: admin [ a t ] ucptt.com