Take the 2-minute tour ×

i've got two objects:

  • "matrix" that has as properties two ints: "somma" and "tono"


  • "sello" that has as properties one NSArray and one UIImage. and has an init method that initializes the array with 13 different matrix objects.

in my view controller i have instances variables for 20 different "sello" objects. and one mutable array that contains these 20 objects.

so in total i've got 260 "matrix objects" inside this 20 objects.

if i have another instance variable that is an int and is equal to 34 (for example), how can i find 34 between those 260 "somma" and obtain the location?

share|improve this question
You have to iterate through (possibly) ever element in every matrix in order to compare the values. –  trojanfoe Oct 4 '12 at 15:28
how? some suggestion? –  user1643728 Oct 4 '12 at 15:38
using blocks this will be easy.. Little complex but its worth looking and learning. –  ValayPatel Oct 4 '12 at 15:52
do you mean remove all the array structure and replace it with blocks? and after that which one will be a good method for look for the int and localize it ? –  user1643728 Oct 4 '12 at 16:09

2 Answers 2

up vote 0 down vote accepted

If by the name of the array, you mean the property name of the sello object in the array of sello objects, I think the following code will work. This is what I put in the view controller code (matrixArray is the array of matrix objects in a sello object):

- (void)viewDidLoad {
    [super viewDidLoad];
    self.sello1 = [[Sello alloc] init];
    self.sello2 = [[Sello alloc] init];
    self.sello3 = [[Sello alloc] init];
    self.sello4 = [[Sello alloc] init];
    self.sello5 = [[Sello alloc] init];
    self.sello6 = [[Sello alloc] init];
    self.sello7 = [[Sello alloc] init];
    self.sello8 = [[Sello alloc] init];
    self.sello9 = [[Sello alloc] init];
    self.sello10 = [[Sello alloc] init];
    self.arr = [NSArray arrayWithObjects:self.sello1,self.sello2,self.sello3,self.sello4,self.sello5,self.sello6,self.sello7,self.sello8,self.sello9,self.sello10,nil];

-(void)viewDidAppear:(BOOL)animated {
    NSNumber *num = @15;
    for (Sello *aSello in self.arr) {
        for (Matrix *aMatrix in aSello.matrixArray) {
            if (aMatrix.somma == num.intValue) {
               NSInteger indx = [self.arr indexOfObjectPassingTest:^BOOL(Sello *obj, NSUInteger idx, BOOL *stop) {
                   return obj == aSello;
                NSLog(@"%@", [NSString stringWithFormat:@"sello%d",indx+1]);
share|improve this answer
it returns all the indexes –  user1643728 Oct 5 '12 at 10:20
Are you sure that you don't have the same test number in all your matrix objects? In my testing, I only got indexes that had my test number in it (I randomly put a number between 1 and 20 in my matrix objects in their init methods). –  rdelmar Oct 5 '12 at 15:11
the method works perfectly. it was missing a == instead of an = in mine one. thx a lot. –  user1643728 Oct 5 '12 at 17:40

Something like this?

NSNumber * num = [NSNumber numberWithInt:34];
for(Sello * sello in selloArray)
    if([sello.matrices valueForKey:@"somma"] containsObject:num])
        return YES;
    if([sello.matrices valueForKey:@"tono"] containsObject:num])
        return YES;
return NO;
share|improve this answer
thanks a lot. is there a way to return the name of the array that contains it? –  user1643728 Oct 4 '12 at 15:53
@user1643728: Arrays don't normally have names. What are you trying to do? –  Chuck Oct 4 '12 at 16:26

Your Answer


