【发布时间】:2015-07-10 10:30:49
【问题描述】:
我正在从 Obj-C 过渡到 Swift,我认为我对 ? !概念。但是,我在一个为零的字段上遇到了糟糕的访问崩溃。
我正在使用
var cell :ContactIInfoTableViewCell!
cell = tableView.dequeueReusableCellWithIdentifier("notes", forIndexPath: indexPath) as! ContactIInfoTableViewCell
if let clinic = receivedVisitDetails?.clinicNotes {
cell.textLabel?.text = clinic
}
如果为空,则会产生错误。我的理解是 IF 语句会阻止这个问题,但它没有。
我在这里做错了什么?
更新
这是核心数据获取的详细视图。
NSManagedObject 设置为
var receivedVisitDetails: VisitDetails! = nil
上一个视图上的prepareForSeague
let visits:VisitDetails = fetchedResultsController.objectAtIndexPath(indexPath!) as! VisitDetails
taskController.receivedVisitDetails = visits
只有当 ClinicNotes 为空时才会发生崩溃。
【问题讨论】:
-
能否也发布崩溃日志?
-
在上面添加了一个sn-p
-
你能显示设置 receivedVisitDetails 的代码吗?
-
Nit pick(与崩溃无关) - 无需使用
ContactIInfoTableViewCell!,我的意思是!,因为dequeueReusableCellWithIdentifier(_:forIndexPath:)总是返回有效的UITableViewCell,请查看此方法的文档。返回类型为UITableViewCell。 -
嗯,当我阅读您的所有更新/编辑时,如果在您的核心数据模型中是可选的,不应该是您的
clinitNotes类型String?而不是String?
标签: swift