使用 fromCString 从 DS 中读取时解包可选值时意外发现 nil [英] unexpectedly found nil while unwrapping an Optional value while reading from DS with fromCString

查看:163
本文介绍了使用 fromCString 从 DS 中读取时解包可选值时意外发现 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 dbtable 中读取数据,但在表的特定位置出现错误.我的 sql 没问题,因为我已经可以从同一个表中读取,但是在特定行我收到一个错误,我想知道如何处理这个错误.我不是在寻找解决我的数据库问题的解决方案,我只是在寻找处理错误的方法,因此它不会崩溃.

I am reading from a dbtable and get an error at a specific position of the table. My sql is ok, because I could already read from the same table, but at a specific row I get an error and I would like to know howto handle this error. I am not looking for a solution to solve my db-issue, I am just looking for handling the error, so it doesn't crash.

我有以下代码:

let unsafepointer=UnsafePointer<CChar>(sqlite3_column_text(statement, 2));
if unsafepointer != nil {
    sText=String.fromCString(unsafepointer)! // <<<<<< ERROR
} else {
   sText="unsafe text pointer is nil !";
}

我收到一个错误:

致命错误:在展开可选值时意外发现 nil"

"fatal error: unexpectedly found nil while unwrapping an Optional value"

在用<<<<<<<<<<<<代码标记的行错误.

不安全指针的值不是nil:

pointerValue : 2068355072

我该如何处理这个错误,这样我的应用才不会崩溃?

How can I handle this error, so my app is not crashing ?

推荐答案

另一个可能的解决方案是:

Another possible solution is this:

let unsafepointer=UnsafePointer<CChar>(sqlite3_column_text(statement, 2));
var sText = "unsafe text pointer is nil !";
if unsafepointer != nil{
    if let text = String.fromCString(unsafepointer) as String?{
        sText = text;
    }    
}

这篇关于使用 fromCString 从 DS 中读取时解包可选值时意外发现 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆