0

I have got a NSMutableArray that is structured as follow:

{
    {
      AccountNumber:Test1
      Type: Electricity
    }

    {
      AccountNumber:Test2
      Type: Water
    }    

    {
      AccountNumber:Test3
      Type: Water
    }   
}

How to print out the account number that are in Water type?

What I have tried is as follow:

- (NSUInteger)indexOfObjectIdenticalTo:(id)data{ return data; }

But I didn't understand how to do it.

2 Answers 2

5

Check out NSPredicate. It will allow to you essentially define a query and apply it to your array to filter the results. This is much faster than a iterative loop through the array to find what you are looking for. For your example, you would simply need to do the following:

NSString *type = @"Water";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Type == %@", type];
NSArray *results = [myArray filteredArrayUsingPredicate:predicate];
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming each item in the array is a NSDictionary, and all the keys/values are NSStrings, you could do something like this:

for (NSDictionary *dict in myArray) {
    if ([[dict objectForKey:@"Type"] isEqualToString:@"Water"]) {
        NSLog(@"Account number %@", [dict objectForKey:@"AccountNumber"]);
    }
}

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.