我有一个来自 NSArray 的变量内容:let content = application["content"]!
当我打印内容时,我有一个字符串:
print(content)
->My content
但是当我想将我的变量转换为 String 时:let content = application["content"]! as! String
我无法打印我的变量,因为它为空:
print(content)
->Could not cast value of type 'NSNull' (0x1a0507768) to 'NSString' (0x1a0511798).
为什么 ?
更新 :
未转换值时我的数组:
{
"application_title" = "Marina Kaye";
"application_type" = discussions;
"application_type_name" = Discussions;
content = (
{
content = "Le nouvel album de Marina Kaye";
link = "?message_id=118";
},
{
content = "Son album est num\U00e9ro 1 des";
link = "?message_id=131";
},
{
content = "Le nouvel album s'appel";
link = "?message_id=126";
}
);
"content_title" = "Messages utiles";
"content_type" = "useful_messages";
}
转换值时我的数组:
{
"application_title" = "Marina Kaye";
"application_type" = discussions;
"application_type_name" = Discussions;
content = "<null>";
"content_title" = "<null>";
"content_type" = "usefull_messages";
}
我无法将内容转换为 NSArray 并将 content_title 转换为字符串。
我的代码:
let applicationsArray = result["applications"]! as! NSArray
for application in applicationsArray {
let applicationTitle = application["application_title"]! as! String
let applicationType = application["application_type"]! as! String
let applicationTypeName = application["application_type_name"]! as! String
let content = application["content"]! as! NSArray
let contentTitle = application["content_title"]! as! String
let contentType = application["content_type"]! as! String
self.listApplications.append(Application(applicationTitle: applicationTitle, applicationType: applicationType, applicationTypeName: applicationTypeName, content: content, contentTitle: contentTitle, contentType: contentType))
}