简体   繁体   English

获取NSMutableData的长度

[英]Getting length of NSMutableData

I have managed to NSInputStream and read some data to NSMutableData object. 我设法NSInputStream并将一些数据读取到NSMutableData对象。 I am able to put this data into string and NSLog it, however when I try to access its length(I am assuming this is its size in bytes) my app crashes. 我可以将这些数据放入字符串中,然后将其记录到NSLog中,但是当我尝试访问其长度(我假设这是其字节大小)时,我的应用就会崩溃。

        NSString *stringData=[[NSString alloc]initWithData:self.data encoding:NSUTF8StringEncoding];
        NSLog(@"%@ thats data",stringData);//logs out content of data
        NSLog(@"%@ thats data length",[self.data length]);//crashes

So my question is if I call copy on NSMutableDate do I get immutable copy ? 所以我的问题是,如果我在NSMutableDate上调用copy,我会得到不可变的副本吗? Am I tying to access the length in a wrong manner ? 我是否想以错误的方式访问长度?

It's because you are trying to log the length as an object using %@. 这是因为您尝试使用%@将长度记录为对象。 It's not an object, it's an integer, so log it with %i instead: 它不是一个对象,而是一个整数,因此请使用%i记录它:

NSLog(@"%i thats data length",[self.data length]);

Logging an object with %@ tries to call the [... description] method on whatever is passed in. You can imagine the horrors that occur in the application memory when it tries to call that method on a random integer, thinking that it's a pointer to an object. 使用%@记录对象会尝试对传入的任何内容调用[... description]方法。您可以想象当应用程序内存尝试对随机整数调用该方法时在应用程序内存中发生的恐怖情况。指向对象的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM