在没有 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 的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在没有 initWithCString 的情况下将 char* 转换为 NSS

基础教程推荐