将 NSArray 字节转换为 NSData
我正在尝试从 REST 服务下载 Objective-C 中的文件,但遇到一些问题。现在,该服务返回 JSON 格式的 byte[]。我能够使用 json 框架获取数组。
NSURL *url = [NSURL URLWithString:@"http://192.168.10.215:8888/Service/RO/GetAllTheBytes/test.txt"];
NSString *str = [[NSString alloc] initWithContentsOfURL:url];
NSDictionary *jsonDic = [str JSONValue];
NSArray *fileContents = [jsonDic objectForKey:@"fileContents"];
现在我想使用这个字节数组来创建一个文件,但我在创建文件时遇到了问题。我应该将 NSArray
转换为 NSData
吗?如何转换?
I'm trying to download a file in Objective-C from a REST service but having some issues. Right now, the service returns a byte[] in JSON. I was able to get the array by using the json-framework.
NSURL *url = [NSURL URLWithString:@"http://192.168.10.215:8888/Service/RO/GetAllTheBytes/test.txt"];
NSString *str = [[NSString alloc] initWithContentsOfURL:url];
NSDictionary *jsonDic = [str JSONValue];
NSArray *fileContents = [jsonDic objectForKey:@"fileContents"];
Now I want to use this array of bytes to create a file, but I'm having trouble creating the file. Should I convert the NSArray
to NSData
, and how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改您的服务以返回 Base64 字符串。市面上有很多 Objective-C Base64 解码器,其中一些还算不错。
NSNumbers 的 NSArray 确实真的真的很愚蠢,最小的空间开销约为 300%(尽管没有 JRA-11693)。
Change your service to return a Base64 string. There are a bunch of Objective-C Base64 decoders out there, some of which are half-decent.
A NSArray of NSNumbers is really really really stupid, with a minimum space overhead of about 300% (although not quite as bad as JRA-11693).