0

I have an iPhone application where i download information from the internet and put it into an NSString. It works fine, until i download large files and put it into that one string, then i get the error

-[__NSArrayM length]: unrecognized selector sent to instance 0x6b6dc60

At one point i was getting a EXC_BAD_ACCESS error, but now that's not showing up. I'm guessing that it is a memory problem but i dont know how to fix it. Is there a limit to how large a string variable can be? Any suggestions? I should also mention that the error sometimes doesn't show up, but most of the time it does. Thanks in advance.

Nate
  • 31,017
  • 13
  • 83
  • 207
vaskal08
  • 80
  • 1
  • 7

3 Answers3

2

Well, it looks like you're trying to get the length of the array by calling a method called length on an array, but to get the length of an array you use the count method like this for example:

NSInteger numberOfElements = [someArray count];

Hope this helps!

P.S. The length method exists but it is used on NSString objects to get the number of characters in the string.

----UPDATE-----

From Ray Wenderlich's "My App Crashed, Now What?" tutorial:

The error message “unrecognized selector sent to instance XXX” means that the app is trying to call a method that doesn’t exist.

So somewhere in your code, you are calling the length method on an object of type NSArray.

You are actually calling the length method on an object of type NSMutableArray, and you know that from the error because __NSArrayM represents an NSMutableArray object; a regular NSArray object would be represented as __NSArrayI (the suffixed "M" stands for "mutable" while the suffixed "I" stands for "immutable").

I even found a very similar question that has a very similar answer to mine:

NSArrayM length : unrecognized selector sent to instance

  • Heh, I had this before. Somewhere you're doing this: [array length]; but arrays use "count", not "length".
Community
  • 1
  • 1
pasawaya
  • 11,515
  • 7
  • 53
  • 92
  • i havnt called the method 'length' on any arrays, im guessing that since you said length is used to get the number of characters in the string, the method is being used by xcode to count the characters of my NSString. again, is there a limit to how many characters there can be that's maybe causing the problem? – vaskal08 Jul 05 '12 at 00:44
  • The maximum number of characters in an NSString (in iOS 5) is (2^31)-1, which is about 2 GB. – Jesse Rusak Jul 05 '12 at 00:51
  • Im 100% sure i didnt call the method 'length' on any arrays. the only time i call the method length is on an NSString. – vaskal08 Jul 05 '12 at 01:04
1

This is happening because you are trying to cast NSMutableArray to NSString, you can remove this error simply by using objectAtIndex:0.

when we use objectAtIndex:0 ,it returns object and in your case that object is your NSString & hence removes error. e.g.

self.quizTextView.text=[questionTexts objectAtIndex:0];
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
0

Try turning on zombies to get a better error message. It's probably something being deallocated before you expect. You can also try running your app in the "Allocations" or "Leaks" instrument to track down why your objects are being released before you expect.

Community
  • 1
  • 1
Jesse Rusak
  • 56,530
  • 12
  • 101
  • 102
  • Hi, if using ARC, would enabling zombies still would be helpful?, havent tried that when developing on my xcode. – Bazinga Jul 05 '12 at 00:18
  • It could still be helpful, yes, though less likely. You could also post the code that's causing the error you cite. – Jesse Rusak Jul 05 '12 at 00:37
  • after using zombies, i found that i apparently released a string object when calling "response3 = [response3 stringByAppendingString:myData2];" does that make any sense? what can i do about this? – vaskal08 Jul 05 '12 at 00:39
  • That would release the original response3 string, but I don't see how that would cause a problem under ARC. Can you post the surrounding code? – Jesse Rusak Jul 05 '12 at 00:49
  • I would need to post too much to actually get the full picture, so ill just explain what i think is causing the problem. I use an NSURLConnection to download the data, but the data is coming in packets so the method below is being called twice. That is why i append response3 to the data string that is received. -(void)connection:(NSURLConnection *) connection didReceiveData:(NSData *)data{ myData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; response = [response stringByAppendingString:myData]; } – vaskal08 Jul 05 '12 at 00:58
  • If you're not using ARC, you'll need to retain your response object. Either way you might prefer to accumulate the data with an NSMutableData rather than an NSString. (You shouldn't use an NSString because it's less efficient and you shouldn't try to UTF8-decide your data when you don't have all of it, since you might have the first byte of a multi-byte sequence.) – Jesse Rusak Jul 05 '12 at 01:33
  • Wow i didn't realize how stupid i was for using NSString to hold data in the first place..... :P i used NSMutableData and now it works perfectly!!! Thanks so much, you're a life saver!! – vaskal08 Jul 05 '12 at 01:45
  • You're welcome. It's nice to accept answers if you find them useful, and encourages people to answer your questions in the future. – Jesse Rusak Jul 05 '12 at 12:03