[問題] 練習實作時遇到的程式請教

作者: 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