Saturday, June 1, 2013

NSMutableArray removeObject inRange example in Objective C (iOS).


NSMutableArray removeObject inRange

Removes all occurrences within a specified range in the array of a given object.

- (void)removeObject:(id)anObject inRange:(NSRange)aRange

Parameters of [NSMutableArray removeObject inRange]
anObject
The object to remove from the array's content.
aRange
The range from which to remove anObject.
Important: Raises an NSRangeException if aRange exceeds the bounds of the array.

Discussion of [NSMutableArray removeObject inRange]
Matches are determined on the basis of an object’s response to the isEqual: message. If the array does not contain anObject within aRange, the method has no effect (although it does incur the overhead of searching the contents).

NSMutableArray removeObject inRange example.
NSRange r;
 r.location = 5;
 r.length = [someArray count]-5;

 [someArray removeObjectsInRange:r];

Example of [NSMutableArray removeObject inRange].
NSRange rang = NSMakeRange(0, 3);
      [muArray removeObject:obj inRange:rang];

NSMutableArray removeObject inRange example.
int main(int argc, char *argv[]) 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
   
    //添加我们的测试代码 
     
     
    //创建自定义类 
    MyClass *myClass = [[MyClass alloc]init]; 
     
    //创建一个可变的数组长度为10 
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];  
      
     
    //向数组中动态的添加对象 
    [array addObject:@"雨松"]; 
    [array addObject:@"MOMO"]; 
    [array addObject:@"小可爱"]; 
    [array addObject:@"哇咔咔"]; 
 
     
    [array addObject:myClass]; 
     
    //设置一个删除范围 
    NSRange range=NSMakeRange(0,5); 
    //删除元素 
    [array removeObject:myClass inRange:range]; 
     
     
     
    for (NSObject * object in array)  
    { 
        NSLog(@"输出对象数组:%@", object); 
    } 
     
     
    int retVal = UIApplicationMain(argc, argv, nil, nil); 
    [pool release]; 
    return retVal; 
}  

End of NSMutableArray removeObject inRange example article.