0

I have a NSMutableArray of, say, 10 NSMutableArrays. Each of these NSMutableArrays contains different number of NSDictionary objects for which I have made a model class. I get this data from an API response. First time you make an API call, the response will be the whole data. From then on, when API call is made, it returns only data that is modified.

When I get some new data from API response, it contains data that is already in the array of dictionaries but with slight modifications.

Model class's structure is as below:

SubCategory.h

@interface SubCategory : MTLModel <MTLJSONSerializing>

@property (nonatomic, retain) NSString *subCategoryID;
@property (nonatomic, retain) NSString *subCategoryName;
@property (nonatomic, retain) NSString *status;

@end

JSON object returned will look like this:

{
    "sub_category" =             (
                            {
                id = 55ed48123;
                name = "Sub Category 1";
                status = 1;
            },
                            {
                id = 55ed47ed3;
                name = "Sub Category 2";
                status = 1;
            }
      );
}

The 'id' field of new data is same. But its 'name' and 'status' fields are different. I want to replace the data in the NSMutableArray of NSDictionary objects with the new data so that it retains the order of the objects in this array. How can I do this?

KTPatel
  • 1,212
  • 4
  • 19
  • 24
Skywalker
  • 1,590
  • 1
  • 18
  • 36
  • you have NSDictionary as objects of NSMuttableArray..rite? – Sujith Chandran Oct 10 '15 at 07:50
  • 1
    Then loop through the Array and check the value of the NSDictionary for each object. If found then you can easily replace that object of Array with new one. – Sujith Chandran Oct 10 '15 at 07:54
  • Why are you using NSDictionary objects instead of the model instances in the arrays? What is the point of having created the model class? – ZeMoon Oct 10 '15 at 08:04
  • It would be great if you could post the code related to the population of the NSMutableArrays, such as the network calls. – ZeMoon Oct 10 '15 at 08:05
  • I am using model class to populate the array.. sorry my bad.. – Skywalker Oct 10 '15 at 08:07
  • i am using the nsdictionary to populate the model class object and populate the array with the model class object' – Skywalker Oct 10 '15 at 08:09
  • @SujithChandran..i thought about that looping through array..but i didn't want to do that because I am already inside a loop where I have to check for this. So i didn't want to increase the complexity even more and was searching for a better and less complex way... – Skywalker Oct 10 '15 at 08:16
  • 1: Use NSPredicate to create the criteria for the duplicate condition. 2: Use filteredArrayUsingPredicate to get an array of duplicated objects. 3: Remove one object from the duplicated array. 4:Use removeObjectsInArray – user523234 Oct 10 '15 at 08:55
  • thanks @user523234...that worked – Skywalker Oct 10 '15 at 09:28
  • if you put that as answer, i'll mark it as right answer... – Skywalker Oct 10 '15 at 09:28

1 Answers1

1

1: Use NSPredicate to create the criteria for the duplicate condition.

2: Use filteredArrayUsingPredicate to get an array of duplicated objects.

3: Remove one object from the duplicated array.

4: Use removeObjectsInArray to remove the list of all objects in the duplicated array from the original array.

user523234
  • 14,323
  • 10
  • 62
  • 102