Search code examples
iosobjective-carraysindexingnspredicate

obj-c cannot find index from searched array / wrong output


Whatever I do in whatever combination, NSLog always returns a wrong index-number of the successfully searched array-object.

code:

-(void)secondbuttonClickedDivi:(id)sender{
    UIButton *xbutton = (UIButton *)sender;
    // NSLog(@"%@arraysucks",);
    //NSArray *subviews = [self.coview subviews];

    // same bogus oupit of index like in  [self.coview subviews] :
    NSArray *subviews= [[NSArray alloc] initWithObjects:xbutton,nil];
    NSLog(@"%@subarray",subviews);
    NSString *search = [NSString stringWithFormat:@"tag = %ld%@", [sendertag],@";"];
    // test search .
    NSString *dummysearch= @"tag = 54;";
    NSLog(@"%@%@",@" : search", search);
    NSLog(@"%@%@",@" : search",dummysearch);
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subviews CONTAINS %@",search];
    NSLog(@"%@predicate", predicate);
    NSArray *filteredArray = [subviews filteredArrayUsingPredicate:predicate];
    NSString *findtag =  [NSString stringWithFormat:@"%@", predicate];

    NSString *gettag = [findtag substringFromIndex:  24];
    NSString *lastget = [gettag substringToIndex:NSMaxRange([gettag rangeOfComposedCharacterSequenceAtIndex:2])];
    long value = [lastget longLongValue];
    NSLog(@"findtag: %@", gettag);
    NSLog(@"lastget: %@", lastget);

    NSLog(@"%ldvalue",value);
    if(_checkstate==2){
        _checkstate=1;

        if([sender tag]!= value){
            [[subviews objectAtIndex:value]  setTitle:_resetbuttonTitle     forState:UIControlStateNormal];
            xbutton.backgroundColor = [UIColor whiteColor];
        }
    }

    NSLog(@"%@dsendertag",_resetbuttonTitle);
    NSLog(@"%ldsendertag",[sender tag]);
    NSInteger anIndex=[filteredArray indexOfObject:xbutton];
    NSLog(@"%ld : isIndex",anIndex);
}

NSLog output:

 (
  UIButton: 0x7fdf1a422550; frame = (161 281; 42 42); opaque = NO; tag = 48; layer = <CALayer: 0x600000224ee0>>
)subarray
  : searchtag = 48;
  : searchtag = 54;
 subviews CONTAINS "tag = 48;"predicate
findtag:  48;"
 lastget:  48
 48value
 48:6dsendertag
 48sendertag
 9223372036854775807 : isIndex
 1checkstate

Except the bogus index-output all array-search is successful, but what I really need is the index.


Solution

  • You seems to be trying to use NSPredicate to find a view inside the array with its tag. In that case you're doing it incorrectly. You should do that instead:

    NSString *search = [NSString stringWithFormat:@"self.tag = %ld", [sender tag]];
    NSPredicate * predicate = [NSPredicate predicateWithFormat:search];
    NSArray *filteredArray = [subviews filteredArrayUsingPredicate:predicate];
    

    Reference: How to filter a NSArray of subviews by tag using NSPredicate?

    Also, 9223372036854775807 is 0x7FFFFFFFFFFFFFFF in hexadecimal, which is the unsigned representation of -1 in 64 bit variables, which is the case of NSInteger (long int). When indexOfObject: returns -1, that's because the object isn't in the array.

    Basically, filteredArray doesn't contains xbutton since the predicate was done incorrectly. You can confirm that by printing filteredArray with NSLog.