My stacktrace is related to maps..
NSGenericException occurs, if we try modify the array, which is being enumerated... I have taken-care about not modifying the enumerating Array.
for (int k=0;k<[[af factsArray] count];k++)
//here af is my object
{
Facts *f = [[af factsArray] objectAtIndex:k];
//here i'm removing unecessary characters from my point String
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"POINT()"];
NSString *pointsStr = [[[f.factPosition mutableCopy] componentsSeparatedByCharactersInSet:doNotWant] componentsJoinedByString:#""];
NSArray *pieces = [[pointsStr componentsSeparatedByString:#" "] copy];
CGPoint point = CGPointMake([[pieces objectAtIndex:2] floatValue], [[pieces objectAtIndex:1] floatValue]);
NSValue *val = [NSValue valueWithCGPoint:point];
[routePointsArray addObject:val];
}
NSInteger pointsCount = routePointsArray.count;
CLLocationCoordinate2D pointsToUse[pointsCount];
for(int i = 0; i < pointsCount; i++)
{
CGPoint p = [[routePointsArray objectAtIndex:i] CGPointValue];
pointsToUse[i] = CLLocationCoordinate2DMake(p.x,p.y);
}
MKPolyline *myPolyline = [MKPolyline polylineWithCoordinates:pointsToUse count:pointsCount];
[routeOverlays addObject:myPolyline];
NSLog(#"routeOverlays count:%d",[routeOverlays count]);
//adding overlays
dispatch_async(dispatch_get_main_queue(), ^{
[self.map addOverlays:routeOverlays];
});
Can anybody tell me the problem with the code because it crashes sometimes and sometimes doesn't.
here is my stacktrace
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x12a9f7d0> was mutated while being enumerated.'
*** First throw call stack:
(
0 CoreFoundation 0x01a2f5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x017b28b6 objc_exception_throw + 44
2 CoreFoundation 0x01abf3b5 __NSFastEnumerationMutationHandler + 165
3 VectorKit 0x04029d7e -[VKMapModel layoutScene:withContext:] + 3854
4 VectorKit 0x0401fc8e -[VKModelObject layoutSceneIfNeeded:withContext:] + 110
5 VectorKit 0x04028bff -[VKMapModel layoutSceneIfNeeded:withContext:] + 143
6 VectorKit 0x0401fd35 -[VKModelObject layoutSceneIfNeeded:withContext:] + 277
7 VectorKit 0x0403f278 -[VKWorld layoutScene:withContext:] + 56
8 VectorKit 0x0402eac6 -[VKScreenCanvas drawWithTimestamp:] + 358
9 VectorKit 0x04019c15 -[VKMapCanvas drawWithTimestamp:] + 149
10 VectorKit 0x0402e4ee -[VKScreenCanvas onTimerFired:] + 142
11 libobjc.A.dylib 0x017c481f -[NSObject performSelector:withObject:] + 70
12 VectorKit 0x041adbdc -[VGLDisplayLink _displayLinkFired:] + 60
13 QuartzCore 0x045f3fca _ZN2CA7Display15DisplayLinkItem8dispatchEv + 48
14 QuartzCore 0x045f3e86 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 310
15 QuartzCore 0x045f43ab _ZN2CA7Display16TimerDisplayLink8callbackEP16__CFRunLoopTimerPv + 123
16 CoreFoundation 0x019edc46 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
17 CoreFoundation 0x019ed62d __CFRunLoopDoTimer + 1181
18 CoreFoundation 0x019d5698 __CFRunLoopRun + 1816
19 CoreFoundation 0x019d4b33 CFRunLoopRunSpecific + 467
20 CoreFoundation 0x019d494b CFRunLoopRunInMode + 123
21 GraphicsServices 0x02c469d7 GSEventRunModal + 192
22 GraphicsServices 0x02c467fe GSEventRun + 104
23 UIKit 0x0031894b UIApplicationMain + 1225
24 SnapTraq 0x0002029d main + 141
25 libdyld.dylib 0x022c1725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I would like to thank #micantox for his attempt to solve my problem...
Anyways, I have solved my own problem.
I was trying to execute the loop from different threads.. that caused the actual problem.
one thread is add the annotations using the NSArray and other thread is removing those NSArray objects. to avoid this problem i have used
#synchronized(self)
{
// your code goes here
}
Related
I wanted to confirm that the following functionality is supported:
I have a simple app where I am trying to send an array of strings to UIActivityViewController. The array contains an attributed string and also a regular string. When I click on the share button and open the Mail app, I get a crash and error. However when I only send the attributed string by itself, it works.
I have tried this on IOS6 and it works fine on that.
Here is the configuration:
NSAttributedString *appName=[[NSAttributedString alloc] initWithString:#"TasteBank\n"
attributes:#{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont
systemFontSize]],NSForegroundColorAttributeName:[UIColor brownColor]}];
//crash seen when #"test" added to array without that no crash when mail app opened on ios7
NSMutableArray *dataToShare = [[NSMutableArray alloc ] initWithObjects: appName, #"test", nil];
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:^{}];
IS there something wrong with this? Is this configuration supported in IOS7? if so, could this be a bug?
I see the following error when I run on my iPhone 4 with IOS 7:
2013-10-17 11:43:50.609 testing12[2499:60b] -[NSConcreteMutableAttributedString appendString:]: unrecognized selector sent to instance 0x165ae670
2013-10-17 11:43:50.612 testing12[2499:60b] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString appendString:]: unrecognized selector sent to instance 0x165ae670'
First throw call stack:
(0x2ff53f53 0x3a32c6af 0x2ff578e7 0x2ff561d3 0x2fea5598 0x32c389a1 0x328e2f2b 0x328e2bb1 0x328e2791 0x328e23b7 0x328e20dd 0x3284c101 0x3284c101 0x326f9601 0x326f468d 0x326c9a25 0x326c8221 0x2ff1f18b 0x2ff1e65b 0x2ff1ce4f 0x2fe87ce7 0x2fe87acb 0x34ba8283 0x32729a41 0xbd1ed 0x3a834ab7)
I see this error when run on IOS7.0 simulator in XCODE 5:
2013-10-17 11:23:42.054 testing12[3258:a0b] -[NSConcreteMutableAttributedString appendString:]: unrecognized selector sent to instance 0xa181470
2013-10-17 11:23:42.085 testing12[3258:a0b] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableAttributedString appendString:]: unrecognized selector sent to instance 0xa181470'
First throw call stack:
(0 CoreFoundation 0x017345e4 exceptionPreprocess + 180
1 libobjc.A.dylib 0x014b78b6 objc_exception_throw + 44
2 CoreFoundation 0x017d1903 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0172490b forwarding___ + 1019
4 CoreFoundation 0x017244ee _CF_forwarding_prep_0 + 14
5 UIKit 0x008bdfa3 -[UIMailActivity prepareWithActivityItems:] + 2346
6 UIKit 0x008b9242 -[UIActivityViewController _executeActivity] + 424
7 UIKit 0x008ba824 -[UIActivityViewController _performActivity:] + 1359
8 libobjc.A.dylib 0x014c981f -[NSObject performSelector:withObject:] + 70
9 UIKit 0x0063c75a -[UIActivityGroupViewController collectionView:didSelectItemAtIndexPath:] + 148
10 UIKit 0x0083929b -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 605
11 UIKit 0x008516d8 -[UICollectionView _userSelectItemAtIndexPath:] + 189
12 UIKit 0x00851895 -[UICollectionView touchesEnded:withEvent:] + 437
13 libobjc.A.dylib 0x014c9874 -[NSObject performSelector:withObject:withObject:] + 77
14 UIKit 0x00382f92 forwardTouchMethod + 271
15 UIKit 0x00383002 -[UIResponder touchesEnded:withEvent:] + 30
16 libobjc.A.dylib 0x014c9874 -[NSObject performSelector:withObject:withObject:] + 77
17 UIKit 0x00382f92 forwardTouchMethod + 271
18 UIKit 0x00383002 -[UIResponder touchesEnded:withEvent:] + 30
19 UIKit 0x0059dd7f _UIGestureRecognizerUpdate + 7166
20 UIKit 0x00268d4a -[UIWindow _sendGesturesForEvent:] + 1291
21 UIKit 0x00269c6a -[UIWindow sendEvent:] + 1030
22 UIKit 0x0023da36 -[UIApplication sendEvent:] + 242
23 UIKit 0x00227d9f _UIApplicationHandleEventQueue + 11421
24 CoreFoundation 0x016bd8af CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
25 CoreFoundation 0x016bd23b __CFRunLoopDoSources0 + 235
26 CoreFoundation 0x016da30e __CFRunLoopRun + 910
27 CoreFoundation 0x016d9b33 CFRunLoopRunSpecific + 467
28 CoreFoundation 0x016d994b CFRunLoopRunInMode + 123
29 GraphicsServices 0x036859d7 GSEventRunModal + 192
30 GraphicsServices 0x036857fe GSEventRun + 104
31 UIKit 0x0022a94b UIApplicationMain + 1225
32 testing12 0x00002efd main + 141
33 libdyld.dylib 0x01d70725 start + 0
34 ??? 0x00000001 0x0 + 1
)
I don't know if this will help but I fixed my problem with a custom activity provider that just gave a NSString to email types and the attributed string to all the others. The email controller autodetected that the string was HTML and formatted it correctly anyway.
I guess if you can get the same effect from HTML that you can with your attributed string this might help?
#implementation ProductActivityItemProvider
...
- (id)placeholderItem {
return [[NSAttributedString alloc] initWithString:#""];
}
- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
if ([activityType isEqualToString:UIActivityTypeMail])
return [NSString stringWithFormat:#"I shared %#", self.product.title];
return nil;
}
- (id)item {
NSString *raw = [NSString stringWithFormat:#"<html><body>%#<br />%#<br /><b>%#</b></body></html>",
self.product.designer,
self.product.title,
self.product.price];
// The mail controller will (a) autodetect HTML and (b) crash if it's given an attributed string. sigh.
if ([self.activityType isEqualToString:UIActivityTypeMail])
return raw;
NSData *data = [raw dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = #{
NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute : #(NSUTF8StringEncoding)
};
NSAttributedString *text = [[NSAttributedString alloc] initWithData:data
options:options
documentAttributes:nil
error:nil];
return text;
}
#end
Got some fast solution here. Just implement a category:
#implementation NSMutableAttributedString (append)
-(void)appendString:(id)string{
if([string isKindOfClass:[NSString class]]){
[self appendAttributedString:[[NSAttributedString alloc] initWithString:string]];
}else if ([string isKindOfClass:[NSAttributedString class]]){
[self appendAttributedString:string];
}
}
#end
I am a starter in iPhone. I wanted to do case-insensitive search. Database helper used: Core Data.
Following is my code:
-(WebAttendee *) FindAttendeeBy:(NSString *) badgeID_
{
AppDelegate_Shared *delegate1 = [[UIApplication sharedApplication]delegate];
badgeID_=[badgeID_ stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"];
badgeID_=[badgeID_ stringByReplacingOccurrencesOfString:#"'" withString:#"\\'"];
self.managedObjectContext = delegate1.managedObjectContext;
return (WebAttendee *)[self FetchManagedObject:#"WebAttendee" :[NSString stringWithFormat:# "Barcode LIKE [c] %#",badgeID_ ]];
}
-(NSManagedObject *)FetchManagedObject:(NSString *)entity_:(NSString *)predicate_
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entity_ inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicate_];
[request setPredicate:predicate];
NSError *error = nil;
//--------------------------crashes at this step---------------------------------
mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
NSLog(#"FetchResult=%#",mutableFetchResults);
[request release];
if (mutableFetchResults == nil) {
// Handle the error.
}
if([mutableFetchResults count]!=0)
return [mutableFetchResults objectAtIndex:0];
else {
return nil;
}
}
but i am getting the following error:
2012-07-05 12:58:52.917 SpotLighter[963:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unimplemented SQL generation for predicate (Barcode LIKE[c] 1123)'
*** Call stack at first throw:
(
0 CoreFoundation 0x01783be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x018d85c2 objc_exception_throw + 47
2 CoreData 0x00fe4676 -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 1270
3 CoreData 0x00f1ca78 -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 488
4 CoreData 0x00f1c881 -[NSSQLAdapter newSelectStatementWithFetchRequest:] + 49
5 CoreData 0x00f1c72e -[NSSQLCore newRowsForFetchPlan:] + 430
6 CoreData 0x00f1bab5 -[NSSQLCore objectsForFetchRequest:inContext:] + 357
7 CoreData 0x00f1b66e -[NSSQLCore executeRequest:withContext:error:] + 206
8 CoreData 0x00fcb0ec -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1084
9 CoreData 0x00f18807 -[NSManagedObjectContext executeFetchRequest:error:] + 359
10 SpotLighter 0x0000ff26 -[EntityCommands FetchManagedObject::] + 310
11 SpotLighter 0x0001436e -[WebAttendeeController FindAttendeeBy:] + 300
12 SpotLighter 0x0009aac6 -[MapBadge Map:::] + 4673
13 SpotLighter 0x000687d7 -[SpotLighterHomeScreen SpotLighterDataReceived::::] + 1111
14 SpotLighter 0x0002ca83 -[SpotlighterViewController txtNumericBadgeFieldDone:] + 445
15 UIKit 0x0051da6e -[UIApplication sendAction:to:from:forEvent:] + 119
16 UIKit 0x005ac1b5 -[UIControl sendAction:to:forEvent:] + 67
17 UIKit 0x005ae647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
18 UIKit 0x005ad1f4 -[UIControl touchesEnded:withEvent:] + 458
19 UIKit 0x007a8987 _UIGestureRecognizerSortAndSendDelayedTouches + 3609
20 UIKit 0x007a90fc _UIGestureRecognizerUpdateObserver + 927
21 CoreFoundation 0x01764fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
22 CoreFoundation 0x016fa0e7 __CFRunLoopDoObservers + 295
23 CoreFoundation 0x016c2bd7 __CFRunLoopRun + 1575
24 CoreFoundation 0x016c2240 CFRunLoopRunSpecific + 208
25 CoreFoundation 0x016c2161 CFRunLoopRunInMode + 97
26 GraphicsServices 0x01f07268 GSEventRunModal + 217
27 GraphicsServices 0x01f0732d GSEventRun + 115
28 UIKit 0x0052c42e UIApplicationMain + 1160
29 SpotLighter 0x00002168 main + 102
30 SpotLighter 0x000020f9 start + 53
)
terminate called after throwing an instance of 'NSException'
return (WebAttendee *)[self FetchManagedObject:#"WebAttendee" :[NSString stringWithFormat:# "Barcode LIKE [c] %#",badgeID_ ]];
was giving error...
I replaced the above line with
return (WebAttendee *)[self FetchManagedObject:#"WebAttendee" :[NSString stringWithFormat:# "Barcode LIKE [c] '%#'",badgeID_ ]];
and it worked fine!
I am loading some values in UItableview from an array of dictionary values. I then alter the dictionary in the array by adding one more key value object as below
NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
[rowDict setObject:#"download successfull" forKey:#"downloadstatus"];
but after this when I try retrieveing value from dictionary in the array as below
NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
NSString *SelectedState = (NSString*)[rowDict objectForKey:#"downloadstatus"];
it crashes ... can any one help me out to fix this
this is the crash display on my consol
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData objectForKey:]: unrecognized selector sent to instance 0x61a8270'
*** Call stack at first throw:
(
0 CoreFoundation 0x003b0be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x015c15c2 objc_exception_throw + 47
2 CoreFoundation 0x003b26fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00322366 ___forwarding___ + 966
4 CoreFoundation 0x00321f22 _CF_forwarding_prep_0 + 50
5 SifyMyStorage 0x0003b35b -[DownloadListViewController tableView:cellForRowAtIndexPath:] + 314
6 UIKit 0x00ec67fa -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
7 UIKit 0x00ebc77f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
8 UIKit 0x00ed1450 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
9 UIKit 0x00ec9538 -[UITableView layoutSubviews] + 242
10 QuartzCore 0x009f4451 -[CALayer layoutSublayers] + 181
11 QuartzCore 0x009f417c CALayerLayoutIfNeeded + 220
12 QuartzCore 0x009ed37c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
13 QuartzCore 0x009ed0d0 _ZN2CA11Transaction6commitEv + 292
14 QuartzCore 0x00a1d7d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
15 CoreFoundation 0x00391fbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
16 CoreFoundation 0x003270e7 __CFRunLoopDoObservers + 295
17 CoreFoundation 0x002efbd7 __CFRunLoopRun + 1575
18 CoreFoundation 0x002ef240 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x002ef161 CFRunLoopRunInMode + 97
20 GraphicsServices 0x02ead268 GSEventRunModal + 217
21 GraphicsServices 0x02ead32d GSEventRun + 115
22 UIKit 0x00e6142e UIApplicationMain + 1160
23 SifyMyStorage 0x000020b8 main + 102
24 SifyMyStorage 0x00002049 start + 53
)
terminate called after throwing an instance of 'NSException'
See if you have allocated the object and also get the count of dictionary , if the value is being added also see if u have declared NSmutabledictionary or just NSdictionary ... a view at your class having the code would be more helpful to sort out your problem
Ok, a couple things:
-the code you posted is fine. That's not the issue. The (NSString *) cast is unnecessary, but not an issue.
-there is an issue with your NSArray tablelist. If you are really adding only NSMutableDictionary to the array, then either you are doing that wrong, or your array is going out of scope and when you think you are accessing your array, you are accessing something else in memory at that location.
-How are you retaining your reference to tablelist within the controller?
-Change your second code block to this and I'll bet you find your problem (retainCount on one of these object == 0):
NSLog(#"retain count=%d",[tableList retainCount]);
NSMutableDictionary *rowDict = [tableList objectAtIndex:arrayindex];
NSLog(#"retain count=%d",[rowDict retainCount]);
NSString *SelectedState = [rowDict objectForKey:#"downloadstatus"];
you r not alloc the NSMutableDictionary so the NSMutableDictionary is empty and your app crashes.
NSMutableDictionary *rowDict = [[NSMutableDictionary alloc]init];
rowDict = [tableList objectAtIndex:arrayindex];
NSString *SelectedState = [rowDict objectForKey:#"downloadstatus"];
best of luck
I have a highscore.plist file with type NSDictionary of 5 items each containing NSNumber - score and String - Name ,
I want to sort scores in descending order . This is what i am trying to do :
NSString *location = [[NSBundle mainBundle] pathForResource:#"HighScores" ofType:#"plist"];
NSArray *highscoredata = [[NSArray alloc] initWithContentsOfFile:location];
self.sortedhighscores = [highscoredata sortedArrayUsingFunction:intSort context:0];
//intSort function :
NSInteger intSort(id num1, id num2, void *context) {
int v1 = [num1 intValue];
int v2 = [num2 intValue];
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
This is the error i receive :
2011-11-18 12:36:25.574 Test[27723:207] -[__NSCFDictionary intValue]: unrecognized selector sent to instance 0x4b66d50
2011-11-18 12:36:25.576 Test[27723:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary intValue]: unrecognized selector sent to instance 0x4b66d50'
*** Call stack at first throw:
(
0 CoreFoundation 0x00f355a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01089313 objc_exception_throw + 44
2 CoreFoundation 0x00f370bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00ea6966 ___forwarding___ + 966
4 CoreFoundation 0x00ea6522 _CF_forwarding_prep_0 + 50
5 Test 0x000035e8 intSort + 36
6 CoreFoundation 0x00e9c3cf __CFSimpleMergeSort + 591
7 CoreFoundation 0x00e9c1c6 __CFSimpleMergeSort + 70
8 CoreFoundation 0x00e9c06c CFSortIndexes + 268
9 CoreFoundation 0x00ebf42c -[NSArray sortedArrayWithOptions:usingComparator:] + 380
10 CoreFoundation 0x00ebf21a -[NSArray sortedArrayUsingFunction:context:] + 106
11 Test 0x000036f7 -[HighScore_ViewController viewDidLoad] + 192
12 UIKit 0x00372089 -[UIViewController view] + 179
13 UIKit 0x00373a3d -[UIViewController viewControllerForRotation] + 63
14 UIKit 0x0036f988 -[UIViewController _visibleView] + 90
15 UIKit 0x0061193c -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 354
16 UIKit 0x002e981e -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 954
17 UIKit 0x00571619 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 1381
18 UIKit 0x0037665d -[UIViewController presentModalViewController:withTransition:] + 3478
19 Test 0x0000296b -[Test_ViewController ShowLeaderboardsModal:] + 198
20 UIKit 0x002c24fd -[UIApplication sendAction:to:from:forEvent:] + 119
21 UIKit 0x00352799 -[UIControl sendAction:to:forEvent:] + 67
22 UIKit 0x00354c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
23 UIKit 0x003537d8 -[UIControl touchesEnded:withEvent:] + 458
24 UIKit 0x002e6ded -[UIWindow _sendTouchesForEvent:] + 567
25 UIKit 0x002c7c37 -[UIApplication sendEvent:] + 447
26 UIKit 0x002ccf2e _UIApplicationHandleEvent + 7576
27 GraphicsServices 0x0188d992 PurpleEventCallback + 1550
28 CoreFoundation 0x00f16944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
29 CoreFoundation 0x00e76cf7 __CFRunLoopDoSource1 + 215
30 CoreFoundation 0x00e73f83 __CFRunLoopRun + 979
31 CoreFoundation 0x00e73840 CFRunLoopRunSpecific + 208
32 CoreFoundation 0x00e73761 CFRunLoopRunInMode + 97
33 GraphicsServices 0x0188c1c4 GSEventRunModal + 217
34 GraphicsServices 0x0188c289 GSEventRun + 115
35 UIKit 0x002d0c93 UIApplicationMain + 1160
36 Test 0x00001f3c main + 102
37 Test 0x00001ecd start + 53
)
terminate called after throwing an instance of 'NSException'
But when I try to run it on Simulator (iOS) it crashes, Im new to Objective C , I think im not doing it right when comparing NSNumber(scores) in array . Any help would be appreciated . Thanks!
Per your description, highscoredata is an array of NSDictionarys. So the result of your call to sortedArrayUsingFunction:context is that intSort is called to compare dictionaries. As per the error reported on the console, NSDictionary does not implement intValue.
To adjust your code directly, try:
NSInteger intSort(id num1, id num2, void *context) {
// assuming the numerical score is in the dictionary
// under the key 'score'
int v1 = [[num1 objectForKey:#"score"] intValue];
int v2 = [[num2 objectForKey:#"score"] intValue];
...
So that compares two dictionaries by retrieving from each the NSNumber representing the score, and comparing that.
NSNumber actually implements compare: for itself, so a more compact version would be:
NSInteger intSort(id num1, id num2, void *context) {
NSNumber *v1 = [num1 objectForKey:#"score"];
NSNumber *v2 = [num2 objectForKey:#"score"];
return [v1 compare:v2];
}
Building on that, because of the way key paths work in Objective-C, an even shorter version would be:
NSArray *highscoredata = [[NSArray alloc] initWithContentsOfFile:location];
NSSortDescriptor *sortDescriptor =
[NSSortDescriptor sortDescriptorWithKey:#"score" ascending:YES];
self.sortedhighscores = [highscoredata
sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
No need for an intSort function at all — NSDictionary knows how to look something up with the key "score" and the things that it looks up are NSNumbers, which know how to compare themselves.
Sort your highscore data in descending order use this code
NSSortDescriptor* sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:NO selector:#selector(localizedCompare:)];
NSArray* sortedArray = [highScoreArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
Here we use localizedCompare: to compare the strings, and pass NO to the ascending: option to sort in descending order.
It seems that id num1 and id num2 that you are passing to function intSort are not NSNumber. They seems to be NSDictionary object.
So i'm searching my Core Data for an item that is inside the current map's longitude and latitude. Everytime i run the statement it crashes.
the code:
NSError *error = nil;
NSFetchRequest *boutiqueRequest = [[NSFetchRequest alloc] init];
NSPredicate *predicateToRun = nil;
[boutiqueRequest setEntity:[NSEntityDescription entityForName:#"Boutique" inManagedObjectContext:managedObjectContext]];
NSLog(#"NE Longitude: %f", [neCoordLong floatValue]);
NSLog(#"NE Latitude: %f", [neCoordLat floatValue]);
NSLog(#"SW Longitude: %f", [swCoordLong floatValue]);
NSLog(#"SW Latitude: %f", [swCoordLat floatValue]);
NSPredicate *longPredicate = [NSPredicate predicateWithFormat: #"longitude BETWEEN %#", [NSArray arrayWithObjects:neCoordLong, swCoordLong, nil]];
NSPredicate *latPredicate = [NSPredicate predicateWithFormat: #"latitude BETWEEN %#", [NSArray arrayWithObjects:neCoordLat, swCoordLat, nil]];
predicateToRun = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:longPredicate, latPredicate, nil]];
[boutiqueRequest setPredicate:predicateToRun];
NSLog(#"%#", [boutiqueRequest predicate]);
NSArray *results = [managedObjectContext executeFetchRequest:boutiqueRequest error:&error];
As soon as I call the executeFetchRequest it gives me the following crash report
2011-02-24 17:42:43.183 DL2[363:207] NE Longitude: 153.5058
2011-02-24 17:42:43.183 DL2[363:207] NE Latitude: -27.811142
2011-02-24 17:42:43.183 DL2[363:207] SW Longitude: 153.286057
2011-02-24 17:42:43.184 DL2[363:207] SW Latitude: -28.033804
2011-02-24 17:42:43.184 DL2[363:207] longitude BETWEEN {153.5058, 153.2861} AND latitude BETWEEN {-27.81114, -28.0338}
2011-02-24 13:57:18.916 DL2[9628:207] -[NSCFNumber constantValue]: unrecognized selector sent to instance 0x954ba80
2011-02-24 13:57:18.925 DL2[9628:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFNumber constantValue]: unrecognized selector sent to instance 0x954ba80'
*** Call stack at first throw:
(
0 CoreFoundation 0x0121abe9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0136f5c2 objc_exception_throw + 47
2 CoreFoundation 0x0121c6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x0118c366 ___forwarding___ + 966
4 CoreFoundation 0x0118bf22 _CF_forwarding_prep_0 + 50
5 CoreData 0x00e18a0b -[NSSQLSimpleWhereIntermediate _generateSQLBetweenStringInContext:] + 1211
6 CoreData 0x00d48931 -[NSSQLSimpleWhereIntermediate generateSQLStringInContext:] + 897
7 CoreData 0x00d4843d -[NSSQLCompoundWhereIntermediate _generateMulticlauseStringInContext:] + 205
8 CoreData 0x00d48342 -[NSSQLCompoundWhereIntermediate generateSQLStringInContext:] + 770
9 CoreData 0x00d47afa -[NSSQLFetchIntermediate generateSQLStringInContext:] + 122
10 CoreData 0x00e0b31d -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 413
11 CoreData 0x00d43a78 -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 488
12 CoreData 0x00d43881 -[NSSQLAdapter newSelectStatementWithFetchRequest:] + 49
13 CoreData 0x00d4372e -[NSSQLCore newRowsForFetchPlan:] + 430
14 CoreData 0x00d42ab5 -[NSSQLCore objectsForFetchRequest:inContext:] + 357
15 CoreData 0x00d4266e -[NSSQLCore executeRequest:withContext:error:] + 206
16 CoreData 0x00df20ec -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 1084
17 CoreData 0x00d3f807 -[NSManagedObjectContext executeFetchRequest:error:] + 359
18 DL2 0x00021060 -[BoutiqueListViewController getBoutiquesToShow] + 1449
19 DL2 0x0002164f -[BoutiqueListViewController viewDidLoad] + 211
20 UIKit 0x003f265e -[UIViewController view] + 179
21 DL2 0x0001ff4e -[BoutiqueMapTabViewController openList] + 698
22 DL2 0x0001f9f3 -[BoutiqueMapTabViewController tabBar:didSelectItem:] + 110
23 UIKit 0x00542167 -[UITabBar _sendAction:withEvent:] + 283
24 UIKit 0x00344a6e -[UIApplication sendAction:to:from:forEvent:] + 119
25 UIKit 0x003d31b5 -[UIControl sendAction:to:forEvent:] + 67
26 UIKit 0x003d5647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
27 UIKit 0x003d316c -[UIControl sendActionsForControlEvents:] + 49
28 UIKit 0x00344a6e -[UIApplication sendAction:to:from:forEvent:] + 119
29 UIKit 0x003d31b5 -[UIControl sendAction:to:forEvent:] + 67
30 UIKit 0x003d5647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
31 UIKit 0x003d41f4 -[UIControl touchesEnded:withEvent:] + 458
32 UIKit 0x003690d1 -[UIWindow _sendTouchesForEvent:] + 567
33 UIKit 0x0034a37a -[UIApplication sendEvent:] + 447
34 UIKit 0x0034f732 _UIApplicationHandleEvent + 7576
35 GraphicsServices 0x01b3da36 PurpleEventCallback + 1550
36 CoreFoundation 0x011fc064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
37 CoreFoundation 0x0115c6f7 __CFRunLoopDoSource1 + 215
38 CoreFoundation 0x01159983 __CFRunLoopRun + 979
39 CoreFoundation 0x01159240 CFRunLoopRunSpecific + 208
40 CoreFoundation 0x01159161 CFRunLoopRunInMode + 97
41 GraphicsServices 0x01b3c268 GSEventRunModal + 217
42 GraphicsServices 0x01b3c32d GSEventRun + 115
43 UIKit 0x0035342e UIApplicationMain + 1160
44 DL2 0x000028fc main + 102
45 DL2 0x0000288d start + 53
I just can't seem to see the problem. I don't know if i'm too close to it, or if it's something to do with it being a Float or what! Any help would be greatly appreciated
Well i wasn't able to solve why the Between function didn't work - but mysql doesn't allow BETWEEN on floating point numbers either. So i'm going to assume that it's a similar reason.
I changed my code to merely create it's own between statement.
NSPredicate *longPredicate = nil;
NSPredicate *latPredicate = nil;
if ([neCoordLong floatValue] > [swCoordLong floatValue])
{
longPredicate = [NSPredicate predicateWithFormat: #" longitude <= %# AND longitude >= %#", neCoordLong, swCoordLong];
}else {
longPredicate = [NSPredicate predicateWithFormat: #" longitude <= %# AND longitude >= %#", swCoordLong, neCoordLong];
}
if ([neCoordLat floatValue] > [swCoordLat floatValue])
{
latPredicate = [NSPredicate predicateWithFormat: #" latitude <= %# AND latitude >= %#", neCoordLat, swCoordLat];
}else {
latPredicate = [NSPredicate predicateWithFormat: #" latitude <= %# AND latitude >= %#", swCoordLat, neCoordLat];
}
Hopefully this helps someone else
I had a similar error as well. I am pretty sure it is a very low level database related issue. Here is some more information I have -
a. With a inmemory database, I could run a test case with exactly the same data just fine.
b. When I got the error "-[__NSCFNumber constantValue]: unrecognized selector sent to instance 0xa9e4ca0". I did
(lldb) po 0xa9e4ca0
$0 = 178146464 -96.24999189101783
Not sure where 178146464 coming from. I will try the manual "between" now.
I don't see the exact cause either, and you haven't shown the code where the rest of boutiqueRequest's properties are set and the fetch fired (not that it matters unless you set the predicate again or something else funky), but the report
2011-02-24 13:57:18.916 DL2[9628:207] -[NSCFNumber constantValue]: unrecognized selector sent to instance 0x954ba80
indicates you're sending the message -constantValue to an object of class NSCFNumber (a toll-free NSNumber/CFNumber instance), which doesn't handle it. So I suggest you try using only portions of the predicate to find which doesn't and does cause some expression within the predicate to be substituted with an NSCFNumber instance.
At the beginning of your fetch you're declaring
NSPredicate *predicateToRun = nil;
And then you assign it a NSCompoundPredicate to it in
predicateToRun = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:longPredicate, latPredicate, nil]];
See if that solves it.
Rog