0

I have an array of recommendedcar IDs, and another array of allcar IDs. From this, I have to take the recommendedcar images. First, I check whether the recommended carid is in my allcar id; if it is, I select the corresponding car images, and store them into NSArray.

This is the code I am using.

 for (int i=0;i<[listOfCarId count];i++) {
    for (int j=0;j<[_allCarID count];j++) {
        tempAllCarId=[_allCarID objectAtIndex:j];
        tempRecommendedCarId=[listOfCarId objectAtIndex:i];
        if ([tempRecommendedCarId isEqualToString:tempAllCarId]) {  
            _recommendedCarImage=[_allCarImages objectAtIndex:j];
            NSLog(@"finalImage%@",_recommendedCarImage);
        }
    }
}

_recommendedcarImage is NSMUtableArray; I want a NSArray. How can I convert it to a NSArray?

How can i replace the "_recommendedCarImage " with an NSArray?? Currently _recommendedCarImage is a mutable array.

Naveen
  • 1,251
  • 9
  • 20
  • 38
  • From your responses to Hinta, this question is not about how to ... but the troubleshooting of your snippet. Paste the error message in your question and rephrase your question. Incidentally, Hinta's is on the right track. Good luck! – user523234 Apr 06 '13 at 10:33

3 Answers3

4

Polymorphism. Since NSMutableArray is a subclass of NSArray, you can use it anywhere an NSArray is expected. You don't have to do anything.

  • 2
    @Naveen "it is not working" is less than non-informative, and if this is not working, then you are not doing what you are asking for. –  Apr 06 '13 at 08:58
  • *** First throw call stack: (0x1e8c012 0x1963e7e 0x1f174bd 0x1e7bbbc 0x1e7b94e 0x1e69deb 0x14dce 0x14987 0x1466d 0x131e3 0x9891c7 0x989232 0x9894da 0x9a08e5 0x9a09cb 0x9a0c76 0x9a0d71 0x9a189b 0x9a1e93 0x9a1a88 0xcfde63 0xcefb99 0xcefc14 0xda95d9 0xdbb182 0xdbb394 0x1977705 0x9b593c 0x9b59ac 0x1977705 0x9b593c 0x9b59ac 0xb6f1d3 0x1e54afe 0x1e54a3d 0x1e327c2 0x1e31f44 0x1e31e1b 0x3c097e3 0x3c09668 0x8a7ffc 0x268d 0x25b5) libc++abi.dylib: terminate called throwing an exception – Naveen Apr 06 '13 at 09:00
  • can you answer this question http://stackoverflow.com/questions/15847325/toolbar-back-button-is-not-working – Naveen Apr 06 '13 at 09:10
  • 2
    @Naveen Pleas symbolicate it. A moment. –  Apr 06 '13 at 09:25
  • @Naveen It's obvious that you don't retain the array, that's why it is crashing but copying works (since it delegates the ownership to the caller). H2CO3's solution is the correct one, just retain the array. And please read up on memory management in Objective-C, because you will encounter more of these problems and are prone to errors. – JustSid Apr 25 '13 at 06:00
3

Now its working,What i did is , I just copy the contents of Mutable array to NSarray

 recommendedArray=[_recommendedCarImage copy];
Naveen
  • 1,251
  • 9
  • 20
  • 38
1

An NSMutableArray is an NSArray already (as it's a subclass of NSArray), still you can do:

NSArray *array = [NSArray arrayWithArray:mutableArray];
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
  • Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString count]: unrecognized selector sent to instance – Naveen Apr 06 '13 at 09:01
  • @Naveen Either `listOfCarId` or `_allCarID` is `NSString` not `NSArray`. – Baby Groot Apr 06 '13 at 09:04
  • both are NSArray ,otherwise why should inask ths questian – Naveen Apr 06 '13 at 09:09
  • @Naveen because your error says so.... `count` function you can use on array but not on string. Your error says you are performing count function on string somewhere. – Baby Groot Apr 06 '13 at 09:39
  • check your variable type before this loop. http://stackoverflow.com/questions/1144629/in-objective-c-how-do-i-test-the-object-type – Baby Groot Apr 06 '13 at 09:43