Question

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 ?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top