在没有 initWithCString 的情况下将 char* 转换为 NSString 的正确方法?
我正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我可以看到您的第一个日志行中的破折号 (
Retrieved Cravin Chicken Sandwich ...
) 不是一个简单的 ASCIIHYPHEN-MINUS
(U+002D, UTF- 8 2D)。它是 UnicodeEN 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 ASCIIHYPHEN-MINUS
(U+002D, UTF-8 2D). It's a UnicodeEN 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 returnsnil
.Try printing a hex dump of
menuNameChars
. You can do that by setting a breakpoint on the line after the call tosqlite3_column_text
. When the breakpoint is reached, right-click/control-clickmenuNameChars
in your stack frame window and chooseView memory of "*menuNameChars"
(note the*
).数据似乎未编码为 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.尝试一下 stringWithFormat 的强大功能:
try the power of
stringWithFormat:
如果您仔细观察 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+.