Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import <Foundation/Foundation.h>
- @interface Test: NSObject
- @property NSMutableArray *elements;
- -(void) method;
- @end
- @implementation Test
- @synthesize elements;
- -(void) method{
- elements= [[NSMutableArray alloc] init];
- [elements addObjectsFromArray:@[@"c", @"a", @"b", @"c", @"c", @"d", @"c", @"c", @"e", @"c"]];
- NSLog(@"number of elements initially- %d", elements.count);
- int index= elements.count-1;
- for(id i in [elements reverseObjectEnumerator])
- {
- // static int index= elements.count-1;
- NSLog(@"index- %d", index);
- if([i isEqual:@"c"]){
- [elements removeObjectAtIndex:index];
- NSLog(@"Object in place of removed object- %@, at index- %d", [elements objectAtIndex:index], index);
- NSLog(@"No. of elements left after the removal- %d", elements.count);
- }
- index--;
- }
- }
- @end
- int main()
- {
- Test *test= [[Test alloc] init];
- [test method];
- NSLog(@"elements- %@", test.elements);
- }
- cd '~/Desktop/Source Code/'; clang -framework Foundation -w ObjectRemovalInArray.m -o ~/Desktop/Binaries/ObjectRemovalInArray && ~/Desktop/Binaries/ObjectRemovalInArray
- 2018-06-18 20:25:25.797 ObjectRemovalInArray[4684:731973] number of elements initially- 10
- 2018-06-18 20:25:25.797 ObjectRemovalInArray[4684:731973] index- 9
- 2018-06-18 20:25:25.798 ObjectRemovalInArray[4684:731973] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 9 beyond bounds [0 .. 8]'
- *** First throw call stack:
- (
- 0 CoreFoundation 0x00007fff345f632b __exceptionPreprocess + 171
- 1 libobjc.A.dylib 0x00007fff5b764c76 objc_exception_throw + 48
- 2 CoreFoundation 0x00007fff34637634 _CFThrowFormattedException + 202
- 3 CoreFoundation 0x00007fff34515290 __CFStringDecodeByteStream3 + 0
- 4 ObjectRemovalInArray 0x00000001007dbbaf -[Test method] + 799
- 5 ObjectRemovalInArray 0x00000001007dbd54 main + 68
- 6 libdyld.dylib 0x00007fff5c37e015 start + 1
- 7 ??? 0x0000000000000001 0x0 + 1
- )
- libc++abi.dylib: terminating with uncaught exception of type NSException
- Abort trap: 6
Add Comment
Please, Sign In to add comment