iPhone,sqlite3,如何检测空日期,将其分配给NSString时出错? [英] iPhone, sqlite3, how do detect a null date, getting error when assigning to a NSString?

查看:29
本文介绍了iPhone,sqlite3,如何检测空日期,将其分配给NSString时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

NSString *enddate = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_text(statement, 3)];                

我需要输入空日期,但是当向此行传递空值时,我会得到一个错误

I need to have null dates, but when a null is passed to this line, I get an error

*由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[NSPlaceholderString initWithUTF8String:]:NULL cString'

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSPlaceholderString initWithUTF8String:]: NULL cString'

推荐答案

您无法使用NULL指针初始化 NSString 对象,因此只能检查该条件:

You cannot initialize NSString objects with a NULL pointer so just check for that condition:

const char* date = (const char*)sqlite3_column_text(statement, 3);
NSString *enddate = date == NULL ? nil : [[NSString alloc] initWithUTF8String:date];

这篇关于iPhone,sqlite3,如何检测空日期,将其分配给NSString时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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