SIGABRT while adding objects to an NSMutableArray - objective-c

I have a method which returns an NSArray with points to be plotted. Following is the code for my method.
- (NSArray *)pointsToPlot:(GraphView *)requester
{
NSMutableArray *points = [[NSSet alloc] init];
CGPoint midPoint;
midPoint.x = self.graph.bounds.origin.x + self.graph.bounds.size.width / 2;
midPoint.y = self.graph.bounds.origin.y + self.graph.bounds.size.height / 2;
//Find points to plot
NSValue *point1 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x - 10, midPoint.y)];
NSValue *point2 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x, midPoint.y - 10)];
NSValue *point3 = [NSValue valueWithCGPoint:CGPointMake(midPoint.x + 10, midPoint.y)];
[points addObject:point1];
[points addObject:point2];
[points addObject:point3];
[points autorelease];
return points;
}
I am getting SIGABRT as soon as I add point1 to the points array.
Following is the stack trace obtained:
2011-10-27 10:43:36.939 Graphing Calculator[7056:207] -[__NSSet0 addObject:]: unrecognized selector sent to instance 0x4b57900
2011-10-27 10:43:36.943 Graphing Calculator[7056:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSet0 addObject:]: unrecognized selector sent to instance 0x4b57900'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc95a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1d313 objc_exception_throw + 44
2 CoreFoundation 0x00dcb0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d3a966 ___forwarding___ + 966
4 CoreFoundation 0x00d3a522 _CF_forwarding_prep_0 + 50
5 Graphing Calculator 0x000073cd -[GraphViewController pointsToPlot:] + 1037
6 Graphing Calculator 0x00006895 -[GraphView drawRect:] + 965
7 UIKit 0x00053187 -[UIView(CALayerDelegate) drawLayer:inContext:] + 426
8 QuartzCore 0x016b3b5e -[CALayer drawInContext:] + 143
9 QuartzCore 0x016bfe47 _ZL16backing_callbackP9CGContextPv + 85
10 QuartzCore 0x0160d1f7 CABackingStoreUpdate + 2246
11 QuartzCore 0x016bfd24 -[CALayer _display] + 1085
12 QuartzCore 0x016b627d CALayerDisplayIfNeeded + 231
13 QuartzCore 0x0165b0c3 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 325
14 QuartzCore 0x0165c294 _ZN2CA11Transaction6commitEv + 292
15 QuartzCore 0x0165c46d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
16 CoreFoundation 0x00daa89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
17 CoreFoundation 0x00d3f6e7 __CFRunLoopDoObservers + 295
18 CoreFoundation 0x00d081d7 __CFRunLoopRun + 1575
19 CoreFoundation 0x00d07840 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x00d07761 CFRunLoopRunInMode + 97
21 GraphicsServices 0x010011c4 GSEventRunModal + 217
22 GraphicsServices 0x01001289 GSEventRun + 115
23 UIKit 0x00029c93 UIApplicationMain + 1160
24 Graphing Calculator 0x000021d9 main + 121
25 Graphing Calculator 0x00002155 start + 53
)
terminate called after throwing an instance of 'NSException'
Could someone please explain me what am I doing wrong here?

Change
NSMutableArray *points = [[NSSet alloc] init];
to
NSMutableArray *points = [[NSMutableArray alloc] init];

You are creating and initializing an NSSet and storing it to a reference for NSMutableArray.
Change the following line:
NSMutableArray *points = [[NSSet alloc] init];
To this line:
NSMutableArray *points = [[NSMutableArray alloc] initWithCapacity:3];

Related

Application Crash on ios11 & Xcode 9: while adding [self addChildViewController:]

While adding child viewcontroller, I am getting following crash,
Here NavigationViewController is extended from UIViewController.
This error is coming when I upgraded to Xcode 9, working successfully on earlier versions of Xcode.
I referred the Link but not getting any satisfied answer, please help me out this,
Thanx in advance. :)
The block of code is:
_mainSectionsController = [[[MainSectionsController alloc] initWithNibName:nil bundle:nil] autorelease];
[self addChildViewController:_mainSectionsController];
**Init method of _mainSectionsController** .
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (!self) return nil;
self.edgesForExtendedLayout = UIRectEdgeAll;
self.viewControllers = #
[
[[[NavigationViewController alloc] initWithNibName:nil bundle:nil]
autorelease]
];
return self;
}
**init method of NavigationViewController**
-(id)initWithNibName:(NSString*)nibNameOrNil bundle: (NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (!self) return nil;
self.edgesForExtendedLayout = UIRectEdgeAll;
_navController = [[[UINavigationController alloc] initWithNibName:nil bundle:nil] autorelease];
_navController.get().delegate = self;
_navController.get().navigationBarHidden = YES;
_navController.get().view.clipsToBounds = YES;
_navController.get().view.backgroundColor = [UIColor clearColor];
return self;
}
2018-05-22 10:46:23.112290+0530 [912:19925] -[NavigationViewController _viewControllerSubtreeDidGainViewController:]: unrecognized selector sent to instance 0x7f91ba83c000
2018-05-22 10:46:24.939903+0530 [912:19925] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NavigationViewController _viewControllerSubtreeDidGainViewController:]: unrecognized selector sent to instance 0x7f91ba83c000'
* First throw call stack:
(
0 CoreFoundation 0x000000010f03f12b exceptionPreprocess + 171 .
1 libobjc.A.dylib 0x000000010e640f41 objc_exception_throw + 48
2 CoreFoundation 0x000000010f0c0024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 UIKit 0x000000010b2cdf51 -[UIResponder doesNotRecognizeSelector:] + 295
4 CoreFoundation 0x000000010efc1f78 ___forwarding_ + 1432
5 CoreFoundation 0x000000010efc1958 _CF_forwarding_prep_0 + 120
6 UIKit 0x000000010b246c1d -[UIViewController _addChildViewController:performHierarchyCheck:notifyWillMove:] + 696
7 UIKit 0x000000010b26930e -[UIViewController(UIContainerViewControllerProtectedMethods) addChildViewController:] + 83
8 0x00000001058eeac3 -[MainViewController initWithNibName:bundle:] + 5235
9 0x00000001059876ca -[AppDelegate loadMainViewController] + 122
10 0x0000000105984561 -[AppDelegate application:didFinishLaunchingWithOptions:] + 1137
11 UIKit 0x000000010b091bca -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 299
12 UIKit 0x000000010b093648 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4113
13 UIKit 0x000000010b098aeb -[UIApplication _runWithMainScene:transitionContext:completion:] + 1720
14 UIKit 0x000000010b4626f8 111-[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:]_block_invoke + 924
15 UIKit 0x000000010b8384c8 +[_UICanvas _enqueuePostSettingUpdateTransactionBlock:] + 153
16 UIKit 0x000000010b4622f1 -[__UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] + 249
17 UIKit 0x000000010b462b6b -[__UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] + 696
18 UIKit 0x000000010bde0a69 __82-[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:]_block_invoke + 262
19 UIKit 0x000000010bde0922 -[_UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] + 444
20 UIKit 0x000000010babd9c8 __125-[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:]_block_invoke + 221
21 UIKit 0x000000010bcbcb06 _performActionsWithDelayForTransitionContext + 100
22 UIKit 0x000000010babd88b -[_UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] + 231
23 UIKit 0x000000010b837b25 -[_UICanvas scene:didUpdateWithDiff:transitionContext:completion:] + 392
24 UIKit 0x000000010b09736a -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 523
25 UIKit 0x000000010b672605 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 369
26 FrontBoardServices 0x0000000116cb8cc0 -[FBSSceneImpl _didCreateWithTransitionContext:completion:] + 338
27 FrontBoardServices 0x0000000116cc17b5 __56-[FBSWorkspace client:handleCreateScene:withCompletion:]_block_invoke_2 + 235
28 libdispatch.dylib 0x000000010f87b33d _dispatch_client_callout + 8
29 libdispatch.dylib 0x000000010f8809f3 _dispatch_block_invoke_direct + 592
30 FrontBoardServices 0x0000000116ced498 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK + 24
31 FrontBoardServices 0x0000000116ced14e -[FBSSerialQueue _performNext] + 464
32 FrontBoardServices 0x0000000116ced6bd -[FBSSerialQueue _performNextFromRunLoopSource] + 45
33 CoreFoundation 0x000000010efe2101 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17
34 CoreFoundation 0x000000010f081f71 __CFRunLoopDoSource0 + 81
35 CoreFoundation 0x000000010efc6a19 __CFRunLoopDoSources0 + 185
36 CoreFoundation 0x000000010efc5fff __CFRunLoopRun + 1279
37 CoreFoundation 0x000000010efc5889 CFRunLoopRunSpecific + 409
38 GraphicsServices 0x000000010fed99c6 GSEventRunModal + 62
39 UIKit 0x000000010b09a5d6 UIApplicationMain + 159
40 0x000000010590c331 main + 65
41 libdyld.dylib 0x000000010f8f7d81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Since you have autorelease in your MainSectionsController code I assume that you don't use ARC there. I highly recommend upgrading the code of MainSectionsController and NavigationViewController to ARC. This prevents accidental overreleases that might cause issues like this one.
In addition I'd recommend to move self.viewControllers assignment from the constructor to viewDidLoad if possible.

How to add an SKNode to an SKScene

When I enter this line in initWithSize:Size
[self addChild:world];
I retrive an error Thread 1:signal SIGABRT
world is an SKNode
The children that should be in world are also SKNode's
What am I doing wrong here as when I add the SKNode to the scene I retrieve an error
initWithSize:Size Method:
-(id)initWithSize:(CGSize)size{
if (self = [super initWithSize:size]){
//self.size = CGSizeMake(2048, 1048);
self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
_character = [self createCharacter];
[self addChild:_character];
[self addChild:world];
[self createChunks];
[world addChild:_character];
NSLog(#"Children in world: %#",[world children]);
}
return self;
}
createChunks: Method:
-(void)createChunks{
ProceduralMapGeneration *procedure = [[ProceduralMapGeneration alloc]init];
[procedure allocArrays];
_chunkOne = [procedure createChunk];
_chunkOne.position = CGPointMake(self.scene.size.width/2, self.scene.size.height/2);
[world addChild:_chunkOne];
_chunkTwo = [procedure createChunk];
_chunkTwo.position = CGPointMake(400,200);
[world addChild:_chunkTwo];
}
create chunks adds the chunks to world
world is created here
#implementation MainGame{
SKNode *world;
SKSpriteNode *_character;
SKNode *_chunkOne;
SKNode *_chunkTwo;
ProceduralMapGeneration *categoryBitManager;
}
Stack Trace
*** First throw call stack:
(
0 CoreFoundation 0x019161e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x016958e5 objc_exception_throw + 44
2 CoreFoundation 0x01915fbb +[NSException raise:format:] + 139
3 SpriteKit 0x0117cfca -[SKNode addChild:] + 175
4 Giraffe Rampage 0x0000289d -[MainGame initWithSize:] + 413
5 SpriteKit 0x0115ca09 +[SKScene sceneWithSize:] + 104
6 Giraffe Rampage 0x0000555d -[MainMenu touchesBegan:withEvent:] + 429
7 SpriteKit 0x0116e2bf -[SKView touchesBegan:withEvent:] + 824
8 UIKit 0x00274c0b -[UIWindow _sendTouchesForEvent:] + 386
9 UIKit 0x002759d1 -[UIWindow sendEvent:] + 1117
10 UIKit 0x002475f2 -[UIApplication sendEvent:] + 242
11 UIKit 0x00231353 _UIApplicationHandleEventQueue + 11455
12 CoreFoundation 0x0189f77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
13 CoreFoundation 0x0189f10b __CFRunLoopDoSources0 + 235
14 CoreFoundation 0x018bc1ae __CFRunLoopRun + 910
15 CoreFoundation 0x018bb9d3 CFRunLoopRunSpecific + 467
16 CoreFoundation 0x018bb7eb CFRunLoopRunInMode + 123
17 GraphicsServices 0x0390a5ee GSEventRunModal + 192
18 GraphicsServices 0x0390a42b GSEventRun + 104
19 UIKit 0x00233f9b UIApplicationMain + 1225
20 Giraffe Rampage 0x00005e6d main + 141
21 libdyld.dylib 0x01f5d701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
[self addChild:_character];
...
[world addChild:_character];
You added _character twice.

showing elements of dictionary

I have a plist file that contains array of dictionaries, and I'm trying to show in console the elements of the dictionnaries... here is my code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"validrep" ofType:#"plist"];
descArray = [[NSMutableArray alloc] init];
NSString *key = [NSString stringWithFormat:#"test %i",i+1];
NSMutableArray *tabreponses = [[NSMutableArray arrayWithContentsOfFile:path] retain];
NSDictionary *dictreponses = [NSDictionary dictionaryWithObject:[tabreponses objectAtIndex:i] forKey:key];
[descArray addObject:dictreponses];
NSDictionary *dictionary = [descArray objectAtIndex:i];
NSArray *array = [dictionary objectForKey:key];
NSLog(#"the array is %#, it contains %i elements.",array,[array count]);
Till now everythig is working correctly and this is what the console is showing:
2011-12-14 14:52:33.845 Daltonien[1459:b603] the array is {
rep1 = "1 \U00e9toile derri\U00e8re un quadrillage ";
rep2 = "1 lettre B derri\U00e8re un quadrillage";
rep3 = "1 quadrillage seul ";
}, it contains 3 elements.
but it doesn't work when i try to show the first element of the array, i do it like this:
NSLog(#"the array is %#, it contains %i elements, the first element is %# .",array,[array count],[array objectAtIndex:i]);
This results in an exception:
Daltonien[1478:b603] -[__NSCFDictionary objectAtIndex:]:
unrecognized selector sent to instance 0x4b8cbd0
Daltonien[1478:b603] *** Terminating app due to
uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFDictionary objectAtIndex:]:
unrecognized selector sent to instance 0x4b8cbd0'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dca5a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f1e313 objc_exception_throw + 44
2 CoreFoundation 0x00dcc0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d3b966 ___forwarding___ + 966
4 CoreFoundation 0x00d3b522 _CF_forwarding_prep_0 + 50
5 Daltonien 0x00002cff -[DetailViewController tableView:numberOfRowsInSection:] + 193
6 UIKit 0x004772b7 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
7 UIKit 0x00474d88 -[UITableViewRowData numberOfRows] + 108
8 UIKit 0x00328677 -[UITableView noteNumberOfRowsChanged] + 132
9 UIKit 0x00335708 -[UITableView reloadData] + 773
10 UIKit 0x00332844 -[UITableView layoutSubviews] + 42
11 QuartzCore 0x01d6aa5a -[CALayer layoutSublayers] + 181
12 QuartzCore 0x01d6cddc CALayerLayoutIfNeeded + 220
13 QuartzCore 0x01d120b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
14 QuartzCore 0x01d13294 _ZN2CA11Transaction6commitEv + 292
15 QuartzCore 0x01d1346d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
16 CoreFoundation 0x00dab89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
17 CoreFoundation 0x00d406e7 __CFRunLoopDoObservers + 295
18 CoreFoundation 0x00d091d7 __CFRunLoopRun + 1575
19 CoreFoundation 0x00d08840 CFRunLoopRunSpecific + 208
20 CoreFoundation 0x00d08761 CFRunLoopRunInMode + 97
21 GraphicsServices 0x017211c4 GSEventRunModal + 217
22 GraphicsServices 0x01721289 GSEventRun + 115
23 UIKit 0x002c8c93 UIApplicationMain + 1160
24 Daltonien 0x000020c8 main + 102
25 Daltonien 0x00002059 start + 53
26 ??? 0x00000001 0x0 + 1
)
terminate called throwing an exception
with what should i change objectAtIndex:i
THANX for helping me.
The problem is this:
Your key, #"test1", is actually a dictionary, not an array...
To get the first key / value of the dictionary, you would do the following
[[myDictionary keys] objectAtIndex:0]
[[myDictionary values] objectAtIndex:0]
If you want the first object in your array, you'll want something like this:
[array objectAtIndex:0];
To loop through all of the objects, this will do it:
for (id someObject in array) {
// Do stuff
}

NSMutableArray adding/removing objects + NSUserDefaults

I want to make a "Favorites" in my RSS reader. My RSS parser is parsing a RSS feed to NSMutableArray, and then object "item" is created from part of my rss (selected post).
My code:
//Creating mutable array and adding items:
- (void)viewDidLoad {
if (favoritedAlready == nil) {
favoritedAlready = [[NSMutableArray alloc] init];
[[NSUserDefaults standardUserDefaults] setObject:favoritedAlready forKey:#"favorites"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"избранное с нуля");
}
}
- (void) addToFavorites {
NSMutableArray* favoritedAlready = [[NSUserDefaults standardUserDefaults] objectForKey:#"favorites"];
[favoritedAlready addObject: item];
[[NSUserDefaults standardUserDefaults] setObject:favoritedAlready forKey:#"favorites"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"Добавлено в избранное. В избранном %i статей", [favoritedAlready count]);
}
//Removing items (another View)
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
rssItems = [[NSUserDefaults standardUserDefaults] objectForKey:#"favorites"];
[self.tableView reloadData];
NSLog(#"Загрузилось избранное, %i избранных статей", [rssItems count]);
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Номерок строчки в которой удаляемый объект %i", indexPath.row+1);
[rssItems removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[[NSUserDefaults standardUserDefaults] setObject:rssItems forKey:#"favorites"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
It worked perfectly initially, but when I added and removed items, it started crashing.
Crash logs:
I added object to Favorites and removed it:
2011-09-25 20:14:44.534 ARSSReader[36211:11303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object'
*** Call stack at first throw:
(
0 CoreFoundation 0x015505a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x016a4313 objc_exception_throw + 44
2 CoreFoundation 0x01508ef8 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x01508e6a +[NSException raise:format:] + 58
4 CoreFoundation 0x01547dd1 -[__NSCFArray removeObjectAtIndex:] + 193
5 ARSSReader 0x000a0ced -[FavoritesView tableView:commitEditingStyle:forRowAtIndexPath:] + 173
6 UIKit 0x00876037 -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] + 101
7 UIKit 0x0080b4fd -[UIApplication sendAction:to:from:forEvent:] + 119
8 UIKit 0x0089b799 -[UIControl sendAction:to:forEvent:] + 67
9 UIKit 0x0089dc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
10 UIKit 0x0089c7d8 -[UIControl touchesEnded:withEvent:] + 458
11 UIKit 0x0082fded -[UIWindow _sendTouchesForEvent:] + 567
12 UIKit 0x00810c37 -[UIApplication sendEvent:] + 447
13 UIKit 0x00815f2e _UIApplicationHandleEvent + 7576
14 GraphicsServices 0x01c91992 PurpleEventCallback + 1550
15 CoreFoundation 0x01531944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
16 CoreFoundation 0x01491cf7 __CFRunLoopDoSource1 + 215
17 CoreFoundation 0x0148ef83 __CFRunLoopRun + 979
18 CoreFoundation 0x0148e840 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x0148e761 CFRunLoopRunInMode + 97
20 GraphicsServices 0x01c901c4 GSEventRunModal + 217
21 GraphicsServices 0x01c90289 GSEventRun + 115
22 UIKit 0x00819c93 UIApplicationMain + 1160
23 ARSSReader 0x00001e79 main + 121
24 ARSSReader 0x00001df5 start + 53
)
terminate called throwing an exception(gdb)
But, if I added item, restarted app and then removed it, it worked perfectly.
I added item, restarted app, removed item, and tried to add new item:
2011-09-25 20:19:19.212 ARSSReader[36461:11303] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object'
*** Call stack at first throw:
(
0 CoreFoundation 0x015505a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x016a4313 objc_exception_throw + 44
2 CoreFoundation 0x01508ef8 +[NSException raise:format:arguments:] + 136
3 CoreFoundation 0x01508e6a +[NSException raise:format:] + 58
4 CoreFoundation 0x01547cf1 -[__NSCFArray insertObject:atIndex:] + 209
5 CoreFoundation 0x01544c14 -[__NSCFArray addObject:] + 68
6 ARSSReader 0x00004b35 -[DetailsViewController addToFavorites] + 149
7 UIKit 0x0080b4fd -[UIApplication sendAction:to:from:forEvent:] + 119
8 UIKit 0x00a1dcc3 -[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 156
9 UIKit 0x0080b4fd -[UIApplication sendAction:to:from:forEvent:] + 119
10 UIKit 0x0089b799 -[UIControl sendAction:to:forEvent:] + 67
11 UIKit 0x0089dc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
12 UIKit 0x0089c7d8 -[UIControl touchesEnded:withEvent:] + 458
13 UIKit 0x0082fded -[UIWindow _sendTouchesForEvent:] + 567
14 UIKit 0x00810c37 -[UIApplication sendEvent:] + 447
15 UIKit 0x00815f2e _UIApplicationHandleEvent + 7576
16 GraphicsServices 0x01c91992 PurpleEventCallback + 1550
17 CoreFoundation 0x01531944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
18 CoreFoundation 0x01491cf7 __CFRunLoopDoSource1 + 215
19 CoreFoundation 0x0148ef83 __CFRunLoopRun + 979
20 CoreFoundation 0x0148e840 CFRunLoopRunSpecific + 208
21 CoreFoundation 0x0148e761 CFRunLoopRunInMode + 97
22 GraphicsServices 0x01c901c4 GSEventRunModal + 217
23 GraphicsServices 0x01c90289 GSEventRun + 115
24 UIKit 0x00819c93 UIApplicationMain + 1160
25 ARSSReader 0x00001e79 main + 121
26 ARSSReader 0x00001df5 start + 53
)
terminate called throwing an exception(gdb)
In addToFavorites
NSMutableArray* favoritedAlready = [[NSUserDefaults standardUserDefaults] objectForKey:#"favorites"];
will return an NSArray (it makes no difference it you save a mutable version), not an NSMutableArray
You need to create a mutable version:
NSMutableArray* favoritedAlready = [[[NSUserDefaults standardUserDefaults] objectForKey:#"favorites"] mutableCopy];
You can obviously not add items to a non-mutable array. Also just casting the return value to a NSMutableArray is wrong, it may work or it may not but that is irrelevant.
The issue is that NSUserDefaults only returns immutable arrays, even if you put in a mutable array:
Special Considerations:
The returned array and its contents are immutable, even if the values you originally set were mutable.
Therefore, when you fetch your array from NSUserDefaults, you'll have to convert it by using the mutable copy method, or by creating a new NSMutableArray using the returned NSArray.
You may want to consolidate changes to fewer method calls to NSUserDefaults—perhaps in batches, perhaps at certain time intervals. Have a local array keep track of changes, then write the changes out, rather than grabbing the array from NSUserDefaults first. This will save one more trip to disk and will make your code more efficient as well.

Why am I getting invalidArgument exception while running unit test?

Inside my Table view controller I have following method
-(BOOL)isValidCoordinate:(CLLocationCoordinate2D)coordinate
{
//This is just to make sure exception is always thrown a cut down version of method
[NSException raise:#"Invalid longitude value" format:#"Longitude of %d is invalid", coordinate.longitude];
return TRUE;
}
I am running a unit test against it and I it failing giving following exception
2011-10-04 22:58:43.380 navman2[74159:ec03] -[MapViewController isValidCoordinate:]: unrecognized selector sent to instance 0x5e20830
2011-10-04 22:58:43.382 navman2[74159:ec03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MapViewController isValidCoordinate:]: unrecognized selector sent to instance 0x5e20830'
*** Call stack at first throw:
(
0 CoreFoundation 0x00f555a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x010a9313 objc_exception_throw + 44
2 CoreFoundation 0x00f570bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00ec6966 ___forwarding___ + 966
4 CoreFoundation 0x00ec6522 _CF_forwarding_prep_0 + 50
5 navman2Tests 0x05765b41 -[navman2Tests testExceptionThrownBycoordinatesCheck] + 193
6 CoreFoundation 0x00ec5c7d __invoking___ + 29
7 CoreFoundation 0x00ec5b51 -[NSInvocation invoke] + 145
8 SenTestingKit 0x201043d2 -[SenTestCase invokeTest] + 69
9 SenTestingKit 0x20104aa7 -[SenTestCase performTest:] + 192
10 SenTestingKit 0x201041d3 -[SenTest run] + 88
11 SenTestingKit 0x20106eda -[SenTestSuite performTest:] + 115
12 SenTestingKit 0x201041d3 -[SenTest run] + 88
13 SenTestingKit 0x20106eda -[SenTestSuite performTest:] + 115
14 SenTestingKit 0x201041d3 -[SenTest run] + 88
15 SenTestingKit 0x20106eda -[SenTestSuite performTest:] + 115
16 SenTestingKit 0x201041d3 -[SenTest run] + 88
17 SenTestingKit 0x201067a4 +[SenTestProbe runTests:] + 174
18 Foundation 0x0092e79e __NSFireDelayedPerform + 441
19 CoreFoundation 0x00f368c3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
20 CoreFoundation 0x00f37e74 __CFRunLoopDoTimer + 1220
21 CoreFoundation 0x00e942c9 __CFRunLoopRun + 1817
22 CoreFoundation 0x00e93840 CFRunLoopRunSpecific + 208
23 CoreFoundation 0x00e93761 CFRunLoopRunInMode + 97
24 GraphicsServices 0x016171c4 GSEventRunModal + 217
25 GraphicsServices 0x01617289 GSEventRun + 115
26 UIKit 0x001b5c93 UIApplicationMain + 1160
27 navman2 0x000028b9 main + 121
28 navman2 0x00002835 start + 53
)
terminate called throwing an exception(gdb)
Unit test is following:
#import "navman2Tests.h"
#import "TableViewController.h"
#import "MapViewController.h"
#implementation navman2Tests
- (void)setUp
{
[super setUp];
// Set-up czode here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
- (void) testExceptionThrownBycoordinatesCheck
{
TableViewController *newView2 =[[MapViewController alloc] init];
CLLocationCoordinate2D coordinate;
double lat = 61.2180556;
double lng = -149.9002778;
coordinate.latitude = lat;
coordinate.longitude = lng;
STAssertThrows([newView2 isValidCoordinate:coordinate],#"some text description");
[newView2 release];
}
#end
The error is that you did declare newView2 as a TableViewController not a MapViewController (which has that method), change:
TableViewController *newView2 =[[MapViewController alloc] init];
with:
MapViewController *newView2 =[[MapViewController alloc] init];
anyway I would call the variable newViewController rather than newView2 :P