iPhone stringWithCString 已弃用

发布于 2024-09-10 21:27:44 字数 216 浏览 1 评论 0原文

我使用此代码从 sqlite 数据库读取数据:

keyFromSql = [NSString stringWithCString:(char *)sqlite3_column_text(preparedStatement, 1)];

但编译器给了我标题中写的警告...那么,从 sqlite 检索值的正确且未弃用的方法是什么?

谢谢!

I use this code to read data from sqlite database:

keyFromSql = [NSString stringWithCString:(char *)sqlite3_column_text(preparedStatement, 1)];

but the compiler give me the warning wrote in the title... so, what is the right and not deprecated method to retrieve a value from sqlite?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海螺姑娘 2024-09-17 21:27:45
+(id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc

我认为你通常会使用 NSUTF8StringEncoding,所以你的代码看起来像

keyFromSq1 = [NSString stringWithCString:(char *)sqlite3_column_text(preparedStatement, 1) encoding:NSUTF8StringEncoding];

或者你可以使用

keyFromSq1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(preparedStatement, 1)];
+(id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc

I think you'll typically use NSUTF8StringEncoding, so your code would look like

keyFromSq1 = [NSString stringWithCString:(char *)sqlite3_column_text(preparedStatement, 1) encoding:NSUTF8StringEncoding];

Alternatively you can use

keyFromSq1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(preparedStatement, 1)];
独行侠 2024-09-17 21:27:45

由于 SQLite 始终返回 UTF-8* 编码的字符串, 您可以只使用 +stringWithUTF8String:

const char* res = (const char*)sqlite3_column_text(preparedStatement, 1);
keyFromSql = [NSString stringWithUTF8String:res];

(* 或 UTF-16,如果您使用 sqlite3_column_text16

Since SQLite always return UTF-8* encoded strings, you could just use +stringWithUTF8String:.

const char* res = (const char*)sqlite3_column_text(preparedStatement, 1);
keyFromSql = [NSString stringWithUTF8String:res];

(* or UTF-16, if you use sqlite3_column_text16)

套路撩心 2024-09-17 21:27:45

[[NSString alloc] initWithBytes:字节长度:计数编码:NSUTF8StringEncoding]
优于 stringWithCString:encoding:

Apple 的Document 的推荐不太好,因为原始方法“不会在 NULL 字符处短暂停止”。

[[NSString alloc] initWithBytes:bytes length:count encoding:NSUTF8StringEncoding]
is preferred to stringWithCString:encoding:

Apple's Document's recommendation is not very OK, because the original method "doesn’t stop short at a NULL character."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文