请教一个关于 NSData 的诡异问题

2013-01-15 21:19:09 +08:00
Numbcoder  Numbcoder
首先有一个 NSData 实例 data (有数据)

int m;
int n;

[data getBytes:&m range:NSMakeRange(0, 1)];
[data getBytes:&n length: 1];

为什么 m 得到一个很诡异的数据。n 是正确的数据。
按道理说 m 和 n 应该相等啊?
3371 次点击
所在节点   iDev  iDev
3 条回复
Gal3rielol
Gal3rielol
2013-01-15 21:25:18 +08:00
*range*
The range of bytes in the receiver's data to copy to buffer. The range must lie within the range of bytes of the receiver's data.

rang of bytes
Numbcoder
Numbcoder
2013-01-15 21:28:12 +08:00
@Gal3rielol 先谢了。
这个是 API 文档里面的,但还是不太明白是什么意思,能详细解释一下原因么?
lldong
lldong
2013-01-15 21:35:13 +08:00
m和n实在栈里面的,初始的内容是随机的,你直接写一个byte进去然后读出来可能就会有问题了。你可能需要malloc一块内存然后清零。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/57703

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX