【问题标题】:Crash when using Between Predicate Statement and Float在谓词语句和浮点之间使用时崩溃
【发布时间】:2011-02-24 04:01:55
【问题描述】:

所以我正在我的核心数据中搜索当前地图经度和纬度范围内的项目。每次我运行该语句时它都会崩溃。

代码:

    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] 东北经度:153.5058 2011-02-24 17:42:43.183 DL2[363:207] 东北纬度:-27.811142 2011-02-24 17:42:43.183 DL2[363:207] 西南经度:153.286057 2011-02-24 17:42:43.184 DL2[363:207] 西南纬度:-28.033804 2011-02-24 17:42:43.184 DL2[363:207] {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

我似乎看不出问题所在。我不知道我是否离它太近了,或者它是否与它是一个浮动或什么有关!任何帮助将不胜感激

【问题讨论】:

    标签: iphone core-data floating-point predicate


    【解决方案1】:

    好吧,我无法解决为什么 Between 函数不起作用 - 但 mysql 也不允许在浮点数上使用 BETWEEN。所以我会假设这是一个类似的原因。

    我将代码更改为仅创建它自己的 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];
        }
    

    希望这对其他人有所帮助

    【讨论】:

      【解决方案2】:

      我也有类似的错误。我很确定这是一个非常低级的数据库相关问题。这是我掌握的更多信息 -

      一个。使用内存数据库,我可以使用完全相同的数据运行测试用例。 湾。当我收到错误“-[__NSCFNumber constantValue]: unrecognized selector sent to instance 0xa9e4ca0”时。我做了

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

      不确定 178146464 来自哪里。我现在就试试“之间”的手册。

      【讨论】:

        【解决方案3】:

        我也没有看到确切的原因,而且您还没有显示设置精品请求的其余属性并触发 fetch 的代码(这并不重要,除非您再次设置谓词或其他时髦的东西),但报告

        2011-02-24 13:57:18.916 DL2[9628:207] -[NSCFNumber constantValue]:无法识别的选择器发送到实例 0x954ba80

        表示您将消息 -constantValue 发送到 NSCFNumber 类的对象(免费的 NSNumber/CFNumber 实例),该对象不处理它。因此,我建议您尝试仅使用谓词的一部分来查找哪些不会导致谓词中的某些表达式被替换为 NSCFNumber 实例。

        【讨论】:

        • 已经编辑了我的帖子以包括精品请求的设置和执行,并添加了我的 NSLog 的输出 - 如果我摆脱了两个谓词,它可以工作,如果我摆脱一个它仍然不起作用.该课程在数据库中使用 NSNumber,所以我不确定到底发生了什么!也感谢您的回复
        【解决方案4】:

        在您的 fetch 开始时,您要声明

        NSPredicate *predicateToRun = nil;

        然后你给它分配一个 NSCompoundPredicate 给它

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

        看看能不能解决。 罗格

        【讨论】:

        • 在我发布的答案中,在代码中我仍然有相同的 NSCompoundPredicate 语句,所以这不太可能是问题。当我 NSLog'd 谓词时,结果很好。我发布的解决方案现在有效。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-15
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多