Guest User

Untitled

a guest
Jun 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Test: NSObject
  4. @property NSMutableArray *elements;
  5. -(void) method;
  6. @end
  7.  
  8. @implementation Test
  9. @synthesize elements;
  10. -(void) method{
  11. elements= [[NSMutableArray alloc] init];
  12. [elements addObjectsFromArray:@[@"c", @"a", @"b", @"c", @"c", @"d", @"c", @"c", @"e", @"c"]];
  13. NSLog(@"number of elements initially- %d", elements.count);
  14. int index= elements.count-1;
  15. for(id i in [elements reverseObjectEnumerator])
  16. {
  17. // static int index= elements.count-1;
  18. NSLog(@"index- %d", index);
  19. if([i isEqual:@"c"]){
  20. [elements removeObjectAtIndex:index];
  21. NSLog(@"Object in place of removed object- %@, at index- %d", [elements objectAtIndex:index], index);
  22. NSLog(@"No. of elements left after the removal- %d", elements.count);
  23. }
  24. index--;
  25. }
  26. }
  27. @end
  28.  
  29. int main()
  30. {
  31. Test *test= [[Test alloc] init];
  32. [test method];
  33. NSLog(@"elements- %@", test.elements);
  34. }
  35.  
  36. cd '~/Desktop/Source Code/'; clang -framework Foundation -w ObjectRemovalInArray.m -o ~/Desktop/Binaries/ObjectRemovalInArray && ~/Desktop/Binaries/ObjectRemovalInArray
  37. 2018-06-18 20:25:25.797 ObjectRemovalInArray[4684:731973] number of elements initially- 10
  38. 2018-06-18 20:25:25.797 ObjectRemovalInArray[4684:731973] index- 9
  39. 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]'
  40. *** First throw call stack:
  41. (
  42. 0 CoreFoundation 0x00007fff345f632b __exceptionPreprocess + 171
  43. 1 libobjc.A.dylib 0x00007fff5b764c76 objc_exception_throw + 48
  44. 2 CoreFoundation 0x00007fff34637634 _CFThrowFormattedException + 202
  45. 3 CoreFoundation 0x00007fff34515290 __CFStringDecodeByteStream3 + 0
  46. 4 ObjectRemovalInArray 0x00000001007dbbaf -[Test method] + 799
  47. 5 ObjectRemovalInArray 0x00000001007dbd54 main + 68
  48. 6 libdyld.dylib 0x00007fff5c37e015 start + 1
  49. 7 ??? 0x0000000000000001 0x0 + 1
  50. )
  51. libc++abi.dylib: terminating with uncaught exception of type NSException
  52. Abort trap: 6
Add Comment
Please, Sign In to add comment