swift coredata致命错误:在展开Optional值时意外发现nil

时间:2016-01-12 07:37:11

标签: swift core-data

for result in groups{
        let tmp = result.subject
        print("groups subject is \(tmp)")
}

我得到groups subject is MyGroupsData

我想在tableView cellForRowAtIndex

中使用
        var channel : myEntity?
        groupsData = groups[indexPath.row] as? myEntity
        dataSubject = groupsData!.subject
        print("groups subject is \(dataSubject)")

显示groups subject is nil

原因:fatal error: unexpectedly found nil while unwrapping an Optional value

如何解决?

1 个答案:

答案 0 :(得分:0)

尝试使用

if let subject = groupsData?.subject {
dataSubject = subject
}

问题是groupsData是nil而你正试图获得一个不存在的值。