将 NSArray 字节转换为 NSData

发布于 2024-12-03 17:11:48 字数 486 浏览 4 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-12-10 17:11:48

更改您的服务以返回 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文