我有一个字典,如下所示
{
childViews = (
{
childViews = (
{
childViews = (
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {80.166666666666671, 20.166666666666668}";
};
}
);
properties = {
center = "NSPoint: {160, 110}";
};
},
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {130, 20.166666666666668}";
};
}
);
properties = {
center = "NSPoint: {210, 160}";
};
},
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {130, 20.166666666666668}";
};
}
);
properties = {
center = "NSPoint: {210, 210}";
};
},
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {130, 20.166666666666668}";
};
}
);
properties = {
center = "NSPoint: {210, 260}";
};
},
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {130, 20.166666666666668}";
};
}
);
properties = {
center = "NSPoint: {210, 260}";
};
},
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {130.16666666666666, 20.166666666666668}";
};
}
);
properties = {
center = "NSPoint: {210, 310}";
};
},
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {130, 20.166666666666668}";
};
}
);
properties = {
center = "NSPoint: {210, 360}";
};
},
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {129.83333333333334, 20.166666666666668}";
};
}
);
properties = {
center = "NSPoint: {210, 410}";
};
},
{
childViews = (
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {0, 0}";
};
}
);
properties = {
center = "NSPoint: {207, 500.5}";
};
},
{
childViews = (
);
properties = {
center = "NSPoint: {207, 671.83333333333326}";
};
},
{
childViews = (
);
properties = {
center = "NSPoint: {409.83333333333337, 641}";
};
}
);
properties = {
center = "NSPoint: {217, 368}";
};
},
{
childViews = (
{
childViews = (
);
properties = {
center = "NSPoint: {70, 29.833333333333336}";
};
}
);
properties = {
center = "NSPoint: {80, 716}";
};
}
);
properties = {
center = "NSPoint: {207, 368}";
};
}
);
properties = {
center = "NSPoint: {207, 368}";
};
}
);
properties = {
center = "NSPoint: {207, 368}";
};
当我尝试使用代码将这个字典转换为 NSData 时
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[self toDictionary]
options:NSJSONWritingPrettyPrinted
error:&error];
我收到错误
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteValue)'
我需要帮助来理解可能导致这次崩溃的值(value)。
最佳答案
如 documentation 中所述,唯一可以通过 NSJSONSerialization
直接转换为 JSON 的类型是数组、字典、字符串、数字和空值。您的字典包含 NSValue
实例,这些实例包装了 NSPoint
,它们不能转换为 JSON,因此是异常(exception)。您需要将它们替换为允许的类型之一,将点编码为两个值的数组、字典、字符串等。
从你的字典的描述中可能不清楚的原因是 NSValue
实例是用引号打印的,但它们实际上不是字符串——这只是它们的显示方式。
关于ios - 使用 NSJSONSerialization 将 NSDictionary 转换为 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43581382/