2011-02-24 50 views
2

因此,我正在爲當前地圖的經度和緯度內的項目搜索我的核心數據。每次我運行它崩潰的聲明。在謂詞語句和浮點數之間使用時發生崩潰

代碼:

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]; 

只要我叫executeFetchRequest它給了我下面的崩潰報告

2011-02-24 17:42:43.183 DL2 [363:207] NE經度: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經度:153.286057 2011-02- 24:17:42:43.184 DL2 [363:207] SW緯度:-28.033804 2011-02-24 17:42:43.184 DL2 [363:207]經度BETWEEN {153.5058,153.2861}和北緯{-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 

我似乎無法看到的問題。我不知道我是否過於接近它,或者如果它是做一個浮動或什麼!任何幫助將不勝感激

回答

8

嗯,我無法解決爲什麼Between函數不起作用 - 但mysql不允許在浮點數上使用BETWEEN。所以我會假設這是一個類似的原因。

我改變了我的代碼,只是在聲明之間創建它自己的代碼。

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]; 
    } 

希望這有助於別人

0

我沒有看到確切的原因,你還沒有顯示的代碼,其中boutiqueRequest的屬性的其餘部分設置和取消觸發(不是它的問題,除非你設置謂詞又或者別的時髦),但報告

2011-02-24 13:57:18.916 DL2 [9628:207] - [NSCFNumber constantValue]:無法識別的選擇發送到實例0x954ba80

表示您正在將消息-constantValue發送給類NSCFNumber(免費NSNumber/CFNumber實例)的對象,該對象不處理它。因此,我建議您嘗試僅使用部分謂詞來查找哪些不是,並且確實會導致謂詞中的某些表達式被替換爲NSCFNumber實例。

+0

已編輯我的文章,包括精確的請求建立和執行,並添加了我的NSLog的輸出 - 如果我擺脫了兩個謂詞的作品,如果我擺脫一個它仍然無法正常工作。該類在數據庫中使用NSNumber,所以我不確定這是怎麼回事!感謝您的回覆 – Brett

0

在你的開始獲取你聲明

NSPredicate *predicateToRun = nil;

然後你給它分配一個NSCompoundPredicate它在

predicateToRun = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:longPredicate, latPredicate, nil]];

看看是否可以解決它。 Rog

+0

在我發佈的答案中,在代碼中,我仍然擁有相同的NSCompoundPredicate語句,因此不太可能出現問題。當我NSLog'd謂詞它出來罰款。我發佈的解決方案現在工作。 – Brett

1

我也有類似的錯誤。我很確定這是一個非常低級的數據庫相關問題。以下是我的一些更多信息 -

a。使用inmemory數據庫,我可以運行一個測試用例,其數據完全相同。 b。當我得到錯誤「 - [__ NSCFNumber constantValue]:無法識別的選擇器發送到實例0xa9e4ca0」。我沒有

(lldb) po 0xa9e4ca0 
$0 = 178146464 -96.24999189101783 

不確定哪裏來自178146464。我會嘗試手動「之間」現在。