NSArray的移除元素、添加元素

2018-01-28  本文已影响0人  牛程程

// 获取元素的位置

- (NSUInteger) indexOfObject:(id)object;

//截取range范围的数组

- (NSArray *) subarrayWithRange:(NSRenge)range;

//在array最后把otherArray中的对象依次添加进去

-(void)arrayByAddingObjectsFromArray:(NSArray *)otherArray; 


```

NSArray *array = [NSArray arrayWithObjects:@'wendy',@'andy',@'tom',@'jonery',@'stany', nil];

NSString *removeString;

removeString = @“tom”;

NSArray *arrayResult;

arrayResult = [self removeWithArray:array WithObject:removeString];

-(NSArray*)removeWithArray:(NSArray*)array WithObject:(id)removeObj

    If(array.count < 1)

  {

    return nil;

  }

    if(removeObj == nil)

    {

      return array;

    }

    NSIntegter removeIndex;

    removeIndex = [array indexOfObject:removeObj];

  NSArray *arrayA;

  NSArray *arrayB;

  NSRange rangeA;

  rangeA = NSMakeRange(0,removeIndex);

  arrayA = [array subarrayWithRange:rangeA];

  If(array.count <= (removeIndex + 1))

  {

      return arrayA;

  }

  else

  {

  NSRange rangeB;

  NSIntegter lengthB;

  NSIntegter indexB;

  IndexB = removeIndex+1;

  lengthB = array.count - indexB;

  rangeB = NSMakeRange(indexB,lengthB);

  arrayB = [array subarrayWithRange:rangeB];

  If(arrayB.count == array.count)

  {

      return arrayB;

  }

  else

  {

      NSArray *arrayResult;

      arrayResult = [arrayA arrayByAddingObjectsFromArray:arrayB];

      return arrayResult;

  }

}

```

上一篇下一篇

猜你喜欢

热点阅读