9

I am trying to remove objects at array starting at index 5 to the end of the list. I have a for loop to do this, however now I discovered

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

The question is what is the anObject here? I only need range as far as I know

2 Answers 2

27

removeObject:inRange: deletes an object within a certain range. This method would be useful if you wanted delete the string @"Hello World" only if it is one of the first 5 elements.

It sounds like what you are trying to do is delete all objects after the 5th element. If that is what you are trying to do, you should use the removeObjectsInRange: method. For example:

 NSRange r;
 r.location = 5;
 r.length = [someArray count]-5;

 [someArray removeObjectsInRange:r];
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use the NSMakeRange(location, length) function to specify the range.
3

You want

- (void)removeObjectsInRange:(NSRange)aRange

Removes from the array each of the objects within a given range.

1 Comment

+1 - Xcode's quick documentation on the side only mentioned that NSMutableOrderedSet had this as a method, so I didn't think it was the right method to use. But then I checked NSMutableArray's documentation and saw it actually had a method with the exact same signature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.