美文网首页
OC数组遍历

OC数组遍历

作者: 通哥 | 来源:发表于2017-01-18 10:41 被阅读0次

    /***************************数组遍历******************************/

    NSArray *array = @[@"111", @"222", @"333", @"444"];

    // 1.for 循环

    for (int i=0; i

    // 2.for in 循环

    for (NSString *str in array) {

    NSLog(@"%@", str);

    }

    // 3.迭代遍历

    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

    if (idx == 1) {

    *stop = YES;

    }

    NSLog(@"%@", obj);

    }];

    /***************************数组中方法******************************/

    Person *p1 = [[Person alloc] init];

    Person *p2 = [[Person alloc] init];

    Person *p3 = [[Person alloc] init];

    Person *p4 = [[Person alloc] init];

    NSArray *array = @[p1, p2, p3, p4];

    // 方法一

    [array enumerateObjectsUsingBlock:^(Person* obj, NSUInteger idx, BOOL * _Nonnull stop) {

    [obj say];

    }];

    // 方法二

    // [array makeObjectsPerformSelector:@selector(say)];

    相关文章

      网友评论

          本文标题:OC数组遍历

          本文链接:https://www.haomeiwen.com/subject/nwivbttx.html