iOS 判断数组array中是否包含元素a,取出a在array中的下标

时间:2017-06-19 15:05:28   收藏:0   阅读:1012

 

 

目前找到来4个解决办法,第三个尤为简单方便

/////////1111111

    NSArray * arr = @[@"1",@"2",@"3",@"4",@"5"];
    //是否包含
    if ([arr containsObject:@"3"]) {
        
        NSInteger index = [arr indexOfObject:@"3"];
        NSLog(@"-1---%ld---",index);
    }
    
    //输出是  2
    


    //////////22222222

    if ([arr indexOfObject:@"5"] != NSNotFound) {
        
        NSInteger inde =[arr indexOfObject:@"5"] ;
        NSLog(@"-2---%ld----",inde);

    }else{
        
        NSLog(@"不存在");
    }
    
    
    //////22222---->>>>3333333
    
    NSInteger inde =[arr indexOfObject:@"5"];

    if (inde != NSNotFound) {
      
        NSLog(@"-2---%ld----",inde);
        
    }else{
        
        NSLog(@"不存在");
    }


///////44444 用for in 来判断

 

原文:http://www.cnblogs.com/xujiahui/p/7048590.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!