在没有 initWithCString 的情况下将 char* 转换为 NSS

Proper way to convert char* to NSString without initWithCString?(在没有 initWithCString 的情况下将 char* 转换为 NSString 的正确方法?)
本文介绍了在没有 initWithCString 的情况下将 char* 转换为 NSString 的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 SQLite3 数据库中的结果列表提取到 UITableView 中.我从数据库中提取文本的代码是这样的:

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);

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

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.

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

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?

推荐答案

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

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.

尝试打印 menuNameChars 的十六进制转储.您可以通过在调用 sqlite3_column_text 之后的行上设置断点来实现.到达断点时,右键单击/控制单击堆栈框架窗口中的 menuNameChars 并选择 View memory of *menuNameChars"(注意 *).

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 *).

这篇关于在没有 initWithCString 的情况下将 char* 转换为 NSString 的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)