作者MIZUYAMA (致命祈愿)
看板MacDev
标题[问题] 练习实作时遇到的程式请教
时间Tue Jul 21 12:57:44 2015
在自学练习时依照课本上的程式码
但是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://webptt.com/cn.aspx?n=bbs/MacDev/M.1437454667.A.548.html
1F:→ stweber2001: tableView.dequeueReusableCellWithIdentifier有可能 07/21 12:59
2F:→ stweber2001: 回传nil, 如果用as!程式就会死给你看 07/21 12:59
3F:→ MIZUYAMA: 范本用as 但是实作时出现错误 检查多次实在不知道哪里错 07/21 13:01
4F:→ stweber2001: 你把错误贴上来可能会比较清楚一点 07/21 13:56
5F:推 gradyzhuo: dequeueReusableCellWithIdentifier(cellIdentifier, f 07/21 15:36
6F:→ gradyzhuo: orIndexPath: indexPath) 一定会有cell, 所以as! 没问 07/21 15:37
7F:→ gradyzhuo: 题,除非没register reuse identifier 不过满好奇是指 07/21 15:37
8F:→ gradyzhuo: 怎样的与预期不同? 07/21 15:37
9F:→ MIZUYAMA: 这就是错误的程式码= = 07/21 21:48
10F:→ MIZUYAMA: Xcode是说as那出问题,执行後会说建立失败......as!可以 07/21 21:52
11F:→ MIZUYAMA: 但是会跟范例结果不同 07/21 21:52
12F:→ gradyzhuo: 我是问跟范例结果不同是指什麽意思?因为as!是Swift 1. 07/22 01:31
13F:→ gradyzhuo: 2 中改变的部分,在1.1之前是使用as 1.2之後改用as! 但 07/22 01:32
14F:→ gradyzhuo: 跟程式运行没有关系,也就是as!就是以前as,所以执行起 07/22 01:33
15F:→ gradyzhuo: 来是会一样的,除非你其他部分写的跟范本不同。所以还 07/22 01:33
16F:→ gradyzhuo: 是有Code会比较清楚。 07/22 01:34
感谢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
17F:→ gradyzhuo: 有时Xcode+Swift会怪怪的 觉得有问题的话 Clean(cmd+sh 07/22 22:37
18F:→ gradyzhuo: ift+k)一下 说不定就正常了 :p 07/22 22:37
19F:推 gradyzhuo: 话说as和as!还是有一些些的不同 所以可以查一下差别在 07/23 01:24
20F:→ gradyzhuo: 那~ 07/23 01:24