正确的方式转换char *到NSString没有initWithCString? [英] Proper way to convert char* to NSString without initWithCString?
问题描述
我正在从SQLite3数据库的结果列表到 UITableView
。我从数据库中拉取文本的代码如下:
char * menuNameChars =(char *)sqlite3_column_text );
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]从DB回收Cravin鸡肉三明治 -
2011-10-24 22:26:54.327 [23974:207]检索的Cravin鸡肉三明治 - 烤,(null)从DB。
2011-10-24 22:26:54.331 [23974:207]从DB取回Jr鸡肉三明治,Jr鸡肉三明治。
2011-10-24 22:26:54.337 [23974:207]检索的Prime-Cut鸡招标 - 3件,Prime-Cut鸡招标 - 3件从DB。现在,如果我用 initWithUTF8String
替换
initWithCString
,代码工作完美。然而,XCode 4.2告诉我, initWithCString
已被弃用。我知道足够了解我不想使用弃用的代码,但如果 initWithUTF8String
不工作,我应该使用什么?
我可以看到你的第一个日志行中的破折号( 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
之后在行上设置断点来实现。当到达断点时,右键单击/控制单击在您的堆栈框架窗口中的 menuNameChars
,并选择查看* menuNameChars的内存
(注意 *
)。
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?
(edited)
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 *
).
这篇关于正确的方式转换char *到NSString没有initWithCString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!