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

看板MacDev作者 (致命祈願)時間10年前 (2015/07/21 12:57), 10年前編輯推噓2(2018)
留言20則, 3人參與, 最新討論串1/1
在自學練習時依照課本上的程式碼 但是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!用了後沒出現問題但跑不出與範本一樣的結果... 想請高手給予一點指導>"< -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.228.23.85 ※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1437454667.A.548.html

07/21 12:59, , 1F
tableView.dequeueReusableCellWithIdentifier有可能
07/21 12:59, 1F

07/21 12:59, , 2F
回傳nil, 如果用as!程式就會死給你看
07/21 12:59, 2F

07/21 13:01, , 3F
範本用as 但是實作時出現錯誤 檢查多次實在不知道哪裏錯
07/21 13:01, 3F

07/21 13:56, , 4F
你把錯誤貼上來可能會比較清楚一點
07/21 13:56, 4F

07/21 15:36, , 5F
dequeueReusableCellWithIdentifier(cellIdentifier, f
07/21 15:36, 5F

07/21 15:37, , 6F
orIndexPath: indexPath) 一定會有cell, 所以as! 沒問
07/21 15:37, 6F

07/21 15:37, , 7F
題,除非沒register reuse identifier 不過滿好奇是指
07/21 15:37, 7F

07/21 15:37, , 8F
怎樣的與預期不同?
07/21 15:37, 8F

07/21 21:48, , 9F
這就是錯誤的程式碼= =
07/21 21:48, 9F

07/21 21:52, , 10F
Xcode是說as那出問題,執行後會說建立失敗......as!可以
07/21 21:52, 10F

07/21 21:52, , 11F
但是會跟範例結果不同
07/21 21:52, 11F

07/22 01:31, , 12F
我是問跟範例結果不同是指什麼意思?因為as!是Swift 1.
07/22 01:31, 12F

07/22 01:32, , 13F
2 中改變的部分,在1.1之前是使用as 1.2之後改用as! 但
07/22 01:32, 13F

07/22 01:33, , 14F
跟程式運行沒有關係,也就是as!就是以前as,所以執行起
07/22 01:33, 14F

07/22 01:33, , 15F
來是會一樣的,除非你其他部分寫的跟範本不同。所以還
07/22 01:33, 15F

07/22 01:34, , 16F
是有Code會比較清楚。
07/22 01:34, 16F
感謝gradyzhuo大的說明 我後來乾脆在跑一次就解決了 當然也因為這次知道as與後來的as!是一樣的意思 至於第一次為什麼跑不出來我也搞不太懂= = 做法都沒有錯啊.... ※ 編輯: MIZUYAMA (61.228.23.85), 07/22/2015 11:00:35 ※ 編輯: MIZUYAMA (61.228.23.85), 07/22/2015 12:26:17

07/22 22:37, , 17F
有時Xcode+Swift會怪怪的 覺得有問題的話 Clean(cmd+sh
07/22 22:37, 17F

07/22 22:37, , 18F
ift+k)一下 說不定就正常了 :p
07/22 22:37, 18F

07/23 01:24, , 19F
話說as和as!還是有一些些的不同 所以可以查一下差別在
07/23 01:24, 19F

07/23 01:24, , 20F
那~
07/23 01:24, 20F
文章代碼(AID): #1LhT5BL8 (MacDev)
文章代碼(AID): #1LhT5BL8 (MacDev)