传递某些 base64 字符串时 NSData returns nil
NSData returns nil when passing certain base64 strings
我用
[[NSData alloc] initWithBase64EncodedString:content options:0];
加载通过网络传递的二进制数据,作为 JSON 对象的键。有时它有效,但在其他情况下它失败了,比如 this string.
这是NSData
自己在另一端创建的字符串:
[data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
有没有我必须通过的选项,为什么它返回零? String 的长度是 4 的倍数并且没有奇怪的字符,所以我不明白为什么会失败?
我又看了一遍文档,特别是这部分:
The default implementation of this method will reject non-alphabet
characters, including line break characters. To support different
encodings and ignore non-alphabet characters, specify an options value
of NSDataBase64DecodingIgnoreUnknownCharacters.
它拒绝字符的事实意味着它显然会因为这些字符而拒绝整个字符串,因为当我将 NSDataBase64DecodingIgnoreUnknownCharacters
添加到初始化程序选项时,它甚至可以使用提到的字符串。
我用
[[NSData alloc] initWithBase64EncodedString:content options:0];
加载通过网络传递的二进制数据,作为 JSON 对象的键。有时它有效,但在其他情况下它失败了,比如 this string.
这是NSData
自己在另一端创建的字符串:
[data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
有没有我必须通过的选项,为什么它返回零? String 的长度是 4 的倍数并且没有奇怪的字符,所以我不明白为什么会失败?
我又看了一遍文档,特别是这部分:
The default implementation of this method will reject non-alphabet characters, including line break characters. To support different encodings and ignore non-alphabet characters, specify an options value of NSDataBase64DecodingIgnoreUnknownCharacters.
它拒绝字符的事实意味着它显然会因为这些字符而拒绝整个字符串,因为当我将 NSDataBase64DecodingIgnoreUnknownCharacters
添加到初始化程序选项时,它甚至可以使用提到的字符串。