V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
Numbcoder
V2EX  ›  iDev

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

  •  
  •   Numbcoder · 2013-01-15 21:19:09 +08:00 · 3127 次点击
    这是一个创建于 4126 天前的主题,其中的信息可能已经有所发展或是发生改变。
    首先有一个 NSData 实例 data (有数据)

    int m;
    int n;

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

    为什么 m 得到一个很诡异的数据。n 是正确的数据。
    按道理说 m 和 n 应该相等啊?
    3 条回复    1970-01-01 08:00:00 +08:00
    Gal3rielol
        1
    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
        2
    Numbcoder  
    OP
       2013-01-15 21:28:12 +08:00
    @Gal3rielol 先谢了。
    这个是 API 文档里面的,但还是不太明白是什么意思,能详细解释一下原因么?
    lldong
        3
    lldong  
       2013-01-15 21:35:13 +08:00
    m和n实在栈里面的,初始的内容是随机的,你直接写一个byte进去然后读出来可能就会有问题了。你可能需要malloc一块内存然后清零。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2377 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:36 · PVG 19:36 · LAX 04:36 · JFK 07:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.