在没有 initWithCString 的情况下将 char* 转换为 NSString 的正确方法?

发布于 2024-12-11 20:45:50 字数 1054 浏览 0 评论 0原文

我正在将 SQLite3 数据库中的结果列表提取到 UITableView 中。我从数据库中提取文本的代码如下所示:

char *menuNameChars = (char *) sqlite3_column_text(statement, 1);
NSString *menuName = [[NSString alloc] initWithUTF8String:menuNameChars];
NSLog(@"Retrieved %s,%@ from DB.", menuNameChars, menuName);

当我使用 initWithUTF8String 时,有时会从数据库中正确复制信息。但有时,信息可以从 char* 正确显示,但不能从 NSString 显示:

2011-10-24 22:26:54.325 [23974:207] Retrieved Cravin Chicken Sandwich – Crispy, (null) from DB.
2011-10-24 22:26:54.327 [23974:207] Retrieved Cravin Chicken Sandwich – Roast, (null) from DB.
2011-10-24 22:26:54.331 [23974:207] Retrieved Jr Chicken Sandwich, Jr Chicken Sandwich from DB.
2011-10-24 22:26:54.337 [23974:207] Retrieved Prime-Cut Chicken Tenders - 3 Piece, Prime-Cut Chicken Tenders - 3 Piece from DB.

现在,如果我将 initWithUTF8String 替换为 initWithCString,代码完美运行。然而,XCode 4.2 告诉我 initWithCString 已被弃用。我足够了解我不想使用已弃用的代码,但如果 initWithUTF8String 不起作用,我应该使用什么?

I'm pulling a listing of results from a SQLite3 database into a UITableView. The code where I pull text from the database is like so:

char *menuNameChars = (char *) sqlite3_column_text(statement, 1);
NSString *menuName = [[NSString alloc] initWithUTF8String:menuNameChars];
NSLog(@"Retrieved %s,%@ from DB.", menuNameChars, menuName);

When I use initWithUTF8String, sometimes the information is copied properly from the database. Sometimes though, the information is displayed properly from the char*, but not from the NSString:

2011-10-24 22:26:54.325 [23974:207] Retrieved Cravin Chicken Sandwich – Crispy, (null) from DB.
2011-10-24 22:26:54.327 [23974:207] Retrieved Cravin Chicken Sandwich – Roast, (null) from DB.
2011-10-24 22:26:54.331 [23974:207] Retrieved Jr Chicken Sandwich, Jr Chicken Sandwich from DB.
2011-10-24 22:26:54.337 [23974:207] Retrieved Prime-Cut Chicken Tenders - 3 Piece, Prime-Cut Chicken Tenders - 3 Piece from DB.

Now, if I replace initWithUTF8String with initWithCString, the code works perfectly. However, XCode 4.2 tells me that initWithCString has been deprecated. I know enough to understand I don't want to use deprecated code, but if initWithUTF8String isn't working, what should I use?

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

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

发布评论

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

评论(4

征﹌骨岁月お 2024-12-18 20:45:50

我可以看到您的第一个日志行中的破折号 (Retrieved Cravin Chicken Sandwich ...) 不是一个简单的 ASCII HYPHEN-MINUS (U+002D, UTF- 8 2D)。它是 Unicode EN DASH 字符(U+2013、UTF-8 E2 80 93)。第二行也一样。我的猜测是它们在您的数据库中编码不正确。如果您给 -[NSString initWithUTF8String:] 一个不是有效 UTF-8 的 C 字符串,它将返回 nil

尝试打印 menuNameChars 的十六进制转储。您可以通过在调用 sqlite3_column_text 后的行上设置断点来实现此目的。到达断点时,右键单击/按住 Control 键单击堆栈帧窗口中的 menuNameChars,然后选择 查看“*menuNameChars”的内存(请注意 *< /代码>)。

I can see that the dash in your first log line (Retrieved Cravin Chicken Sandwich ...) isn't a simple ASCII HYPHEN-MINUS (U+002D, UTF-8 2D). It's a Unicode EN DASH character (U+2013, UTF-8 E2 80 93). Same for the second line. My guess is they're encoded incorrectly in your database. If you give -[NSString initWithUTF8String:] a C-string that's not valid UTF-8, it returns nil.

Try printing a hex dump of menuNameChars. You can do that by setting a breakpoint on the line after the call to sqlite3_column_text. When the breakpoint is reached, right-click/control-click menuNameChars in your stack frame window and choose View memory of "*menuNameChars" (note the *).

婴鹅 2024-12-18 20:45:50

数据似乎未编码为 UTF-8。您应该找出正在使用的编码,然后使用 initWithCString:encoding: 并传递正确的编码。

It would appear the data is not encoded as UTF-8. You should find out what encoding is being used and then use initWithCString:encoding: and pass the correct encoding.

无人接听 2024-12-18 20:45:50

尝试一下 stringWithFormat 的强大功能:

try the power of stringWithFormat:

最冷一天 2024-12-18 20:45:50

如果您仔细观察 Apple Developer,您会发现实例方法 initWithCString: 仅在没有显式 encoding: 说明符的版本中被弃用。人们可以有充分的理由争论,因此 const char * C 字符串到 NSString 的转换完全有效。请比较:

https://developer.apple.com/documentation/foundation/nsstring /1497394-initwithcstring

到较新的 initWithCString:encoding:

https://developer.apple.com/documentation/foundation/nsstring/ 1411950-initwithcstring?language=objc

此 Foundation API 更改是自 macOS 10.4+ 以来进行的。

If you look closely at Apple Developer it seems that instance method initWithCString: is only deprecated for the version without explicit encoding: specifier. With good reason one could argue and thus perfectly valid for const char * C-string conversion to NSString. Please compare:

https://developer.apple.com/documentation/foundation/nsstring/1497394-initwithcstring

to newer initWithCString:encoding:

https://developer.apple.com/documentation/foundation/nsstring/1411950-initwithcstring?language=objc

This Foundation API change was made since macOS 10.4+.

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