0

I have managed to NSInputStream and read some data to NSMutableData object. 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.

        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 ? Am I tying to access the length in a wrong manner ?

stringCode
  • 2,274
  • 1
  • 23
  • 32
  • 1
    Yes, `copy` gives you an immutable object. `mutableCopy` gives you a mutable object. – edc1591 Feb 02 '12 at 15:46
  • Thank you. Any thoughts on why can I access the length of my data. I tried making a NSData copy of it and calling length app still crashes – stringCode Feb 02 '12 at 15:50

1 Answers1

3

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:

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.

Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103