Naveen
Naveen

Reputation: 1251

How can I move the values in NSMutableArray to NSArray

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.

Upvotes: 0

Views: 856

Answers (3)

Naveen
Naveen

Reputation: 1251

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

 recommendedArray=[_recommendedCarImage copy];

Upvotes: 3

Baby Groot
Baby Groot

Reputation: 4279

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

NSArray *array = [NSArray arrayWithArray:mutableArray];

Upvotes: 1

user529758
user529758

Reputation:

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

Upvotes: 4

Related Questions