无论我以何种组合进行操作,NSLog始终返回成功搜索的数组对象的错误索引号。

码:

-(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输出:

(
UIButton:0x7fdf1a422550;框架=(161281; 42 42);不透明=不;标签= 48;层= >
)子数组
:searchtag = 48;
:searchtag = 54;
子视图包含“标签= 48;”谓词
findtag:48;”
的lastget:48
48值
48:6dsendertag
48个发件人标签
9223372036854775807:isIndex
1检查状态

除了伪造的索引输出之外,所有数组搜索都是成功的,但是我真正需要的是索引。

最佳答案

您似乎正在尝试使用NSPredicate在带有标签的数组内查找视图。在这种情况下,您做错了。您应该改为:

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

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

此外,9223372036854754775807的十六进制值为0x7FFFFFFFFFFFFFFFFF,这是64位变量中-1的无符号表示,这是NSInteger(长整数)的情况。当indexOfObject:返回-1时,这是因为该对象不在数组中。

基本上,由于谓词处理不正确,filteredArray不包含xbutton。您可以通过使用NSLog打印filteredArray来确认这一点。

关于ios - obj-c无法从搜索到的数组中找到索引/输出错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40194313/

10-12 05:49