ios - 为什么将 char 数组转换为字符串会创建一个 nil 字符串?

标签 ios objective-c utf-8 nsstring char

<分区>

这是我的字符数组返回的内容:

 char *methodChars = (char *) sqlite3_column_text(statement, 3);

(char *) methodChars = 0x090f5e00 "Start cooking pasta in a large pan of boiling water.\xa0 Meanwhile, spray a large pan with oil and heat.\xa0\xa0 Add the onion and celery and cook over medium heat for 3 minutes or until soft.\xa0 Stir in the corn kernels.\xa0 Put the cornflour into a small bowl and gradually add 80 ml (1/3 cup) milk, stirring until smooth.\xa0 Add the remaining milk and the stock to the pan then gradually add the cornflour mixture, stirring constantly.\xa0 Keep stirring until the sauce boils and thickens.\xa0 Remove from heat and stir in the cheese until melted.\xa0 When the pasta is al dente, drain and add with the tuna to the pan.\xa0 Stir to combine, and serve immediately."

这是我转换它的地方,这是我得到的:

NSString *method = [[NSString alloc] initWithUTF8String:methodChars];

这是我从方法中得到的:

nil

谁能解释一下原因,以及我该如何解决这个问题?

当 methodChars 不包含 \xa0 而只有一个空格时,此代码适用于某些情况。谁能解释为什么这可能是一个原因?

最佳答案

您的字符串未以 UTF-8 编码:它包含字节 \xa0。如果字符串应该包含 U+00A0 不间断空格字符,则它应该被编码为 \xc2\xa0

您可以使用 \xa0 映射到有效字符的编码来创建字符串,例如:

NSString *method = [[NSString alloc] initWithCString:methodChars encoding:NSISOLatin1StringEncoding];

关于ios - 为什么将 char 数组转换为字符串会创建一个 nil 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19259335/

上一篇:ios - 如果我将我的帐户和密码存储在 SQLite 中,Apple 会拒绝我的应用程序吗?

下一篇:ios - 我可以在自身内部编写应用程序 - Xcode/iOS 吗?

相关文章:

ios - IOS 无法创建与已删除相册同名的相册?

ios - 如何避免向任何单元格添加多个点击手势?

iphone - 根据自定义单元格高度动态 uitableviewcell 高度

iphone - UIPickerView 不显示数据

java - 以 Base64 和 utf-8 解码电子邮件主题字符串

django - Django 应用程序中/admin/处的 UnicodeEncodeError

iOS :How to get Facebook Album Photo's Picker

ios - Web 服务调用返回 SIGABRT

sql-server - SQL Server nvarchar N varchar 字段的前缀

ios - 如何使用 Objective-C 初始化嵌套的 Node.js 模式