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?

8
  • Possible duplicate of NSArray find object or objects - best practices Commented Sep 1, 2018 at 21:33
  • Are the sample objects mutable? Commented Sep 1, 2018 at 21:34
  • Yes they are mutable Commented Sep 1, 2018 at 21:39
  • Duplicate of Set bool property of all objects in the array Commented Sep 1, 2018 at 21:46
  • @Willeke this is not duplicate question. Please read carefully before jumping to conclusions Commented Sep 1, 2018 at 22:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.