0

I have constructed my model object with following.

@property (strong, nonatomic) NSString *id;
@property (strong, nonatomic) NSString *somevalue;
@property (strong, nonatomic) NSString *another value;
@property (strong, nonatomic) NSString *xtz;
@property (strong, nonatomic) NSString *st;
@property (strong, nonatomic) NSMutableArray *sc; 

Now my mutable array is filled with following sample objects,

{a = 1;b = 6;c = 0;},{a = 2;b = 7;c = 0;},{a = 2;b = 8;c = 0;},{a = 3;b=9;c = 0;}

There are roughly 200 of such objects in my array. Now I cannot figure out a decent way of looping through the array updating the value of for example 'c' to '1' where 'a' ==1.

I could use a for loop like that:

for(int i = 0 ; i <myobject.sc.count; i++) {
    NSLog(@"%@",[[myobject sc]objectAtIndex:i]);
}

It would allow me to iterate through the array. But still face the problem of looping through values contains within each object. Maybe a nested loop?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
humble_pie
  • 119
  • 10
  • Possible duplicate of [NSArray find object or objects - best practices](https://stackoverflow.com/questions/20024937/nsarray-find-object-or-objects-best-practices) – Willeke Sep 01 '18 at 21:33
  • Are the sample objects mutable? – Willeke Sep 01 '18 at 21:34
  • Yes they are mutable – humble_pie Sep 01 '18 at 21:39
  • Duplicate of [Set bool property of all objects in the array](https://stackoverflow.com/questions/33013868/set-bool-property-of-all-objects-in-the-array) – Willeke Sep 01 '18 at 21:46
  • @Willeke this is not duplicate question. Please read carefully before jumping to conclusions – humble_pie Sep 01 '18 at 22:54
  • Which question is not a duplicate and why? There are many other solutions, for example you could use `enumerateObjectsUsingBlock:` and test `a` and set `c` in the block. Or are the objects arrays? – Willeke Sep 01 '18 at 23:01
  • I am familiar with predicate solution, but if I understand correctly, it will give me a filtered array, But I want update the values in the existing array, rather create a new array – humble_pie Sep 01 '18 at 23:06
  • The filtered array contains the same objects, not duplicates. – Willeke Sep 02 '18 at 08:49

1 Answers1

0

All you need is a simple loop:

for (WhateverDataType *data in myobject.sc) {
    if (data.a == 1) {
        data.c = 1
        // break // uncomment if you only want to update the first match
    }
}

This gives you a general idea for the solution. The specific syntax depends on what type of objects are actually in the mutable array.

rmaddy
  • 314,917
  • 42
  • 532
  • 579