2011-03-11 4 views
7

두 날짜 사이에 startTime이있는 코어 데이터 레코드에서 가져 오려고합니다. 여기 NSDate와 함께 NSPredicate가 발생합니다 - [__ NSDate constantValue] : 인스턴스로 전송 된 인식 할 수없는 선택기 0x1e7ff0

내 코드입니다 :

NSDate *today = [NSDate date]; 
NSDate *distantFuture = [NSDate distantFuture]; 

// Create the predicate 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startTime BETWEEN %@", [NSArray arrayWithObjects:today, distantFuture, nil]]; 

NSLog(@"today: %@, distantFuture: %@", today, distantFuture); 
NSLog(@"predicate: %@", predicate); 

// Add the predicate to the fetchRequest 
[[[self fetchedResultsController] fetchRequest] setPredicate:predicate]; 

NSError *error; 
if (![[self fetchedResultsController] performFetch:&error]) 
{ 
    // Update to handle the error appropriately. 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
    exit(-1); // Fail 
} 

그리고 여기에 콘솔 출력입니다 :

2011-03-11 02:00:03.166 TestApp[2668:307] today: 2011-03-11 01:00:03 AM +0000, distantFuture: 4001-01-01 12:00:00 AM +0000 
2011-03-11 02:00:03.174 TestApp[2668:307] predicate: startTime BETWEEN {2011-03-11 01:00:03 AM +0000, 4001-01-01 12:00:00 AM +0000} 
2011-03-11 02:00:03.180 TestApp[2668:307] -[__NSDate constantValue]: unrecognized selector sent to instance 0x1e7ff0 
2011-03-11 02:00:03.198 TestApp[2668:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate constantValue]: unrecognized selector sent to instance 0x1e7ff0' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x314d0987 __exceptionPreprocess + 114 
    1 libobjc.A.dylib      0x319a149d objc_exception_throw + 24 
    2 CoreFoundation      0x314d2133 -[NSObject(NSObject) doesNotRecognizeSelector:] + 102 
    3 CoreFoundation      0x31479aa9 ___forwarding___ + 508 
    4 CoreFoundation      0x31479860 _CF_forwarding_prep_0 + 48 
    5 CoreData       0x352ed973 -[NSSQLSimpleWhereIntermediate _generateSQLBetweenStringInContext:] + 766 
    6 CoreData       0x35244fff -[NSSQLSimpleWhereIntermediate generateSQLStringInContext:] + 502 
    7 CoreData       0x35244647 -[NSSQLFetchIntermediate generateSQLStringInContext:] + 74 
    8 CoreData       0x352e42b3 -[NSSQLGenerator newSQLStatementForFetchRequest:ignoreInheritance:countOnly:nestingLevel:] + 234 
    9 CoreData       0x352414ab -[NSSQLAdapter _newSelectStatementWithFetchRequest:ignoreInheritance:] + 378 
    10 CoreData       0x35241329 -[NSSQLAdapter newSelectStatementWithFetchRequest:] + 16 
    11 CoreData       0x35241205 -[NSSQLCore newRowsForFetchPlan:] + 288 
    12 CoreData       0x35240531 -[NSSQLCore objectsForFetchRequest:inContext:] + 420 
    13 CoreData       0x35240151 -[NSSQLCore executeRequest:withContext:error:] + 304 
    14 CoreData       0x352d0295 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 896 
    15 CoreData       0x3523d877 -[NSManagedObjectContext executeFetchRequest:error:] + 374 
    16 CoreData       0x352f541b -[NSFetchedResultsController performFetch:] + 766 
    17 TestApp        0x0001ac6d -[MatchesCalendarDataSource loadMatchesFrom:to:delegate:] + 268 
    18 TestApp        0x0001ad51 -[MatchesCalendarDataSource presentingDatesFrom:to:delegate:] + 68 
    19 TestApp        0x00021353 -[KalViewController reloadData] + 110 
    20 TestApp        0x00021b15 -[KalViewController loadView] + 452 
    21 UIKit        0x338dc227 -[UIViewController view] + 30 
    22 UIKit        0x338e8d0b -[UIViewController contentScrollView] + 22 
    23 UIKit        0x338e8b7b -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 30 
    24 UIKit        0x338e8a79 -[UINavigationController _layoutViewController:] + 24 
    25 UIKit        0x338e84d3 -[UINavigationController _startTransition:fromViewController:toViewController:] + 254 
    26 UIKit        0x338e835f -[UINavigationController _startDeferredTransitionIfNeeded] + 182 
    27 UIKit        0x338e82a3 -[UINavigationController viewWillLayoutSubviews] + 14 
    28 UIKit        0x338e823f -[UILayoutContainerView layoutSubviews] + 138 
    29 UIKit        0x338b10cf -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 26 
    30 CoreFoundation      0x3146ebbf -[NSObject(NSObject) performSelector:withObject:] + 22 
    31 QuartzCore       0x30a6c685 -[CALayer layoutSublayers] + 120 
    32 QuartzCore       0x30a6c43d CALayerLayoutIfNeeded + 184 
    33 QuartzCore       0x30a6656d _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 212 
    34 QuartzCore       0x30a66383 _ZN2CA11Transaction6commitEv + 190 
    35 QuartzCore       0x30a89f9d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 52 
    36 CoreFoundation      0x31460c59 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 16 
    37 CoreFoundation      0x31460acd __CFRunLoopDoObservers + 412 
    38 CoreFoundation      0x314580cb __CFRunLoopRun + 854 
    39 CoreFoundation      0x31457c87 CFRunLoopRunSpecific + 230 
    40 CoreFoundation      0x31457b8f CFRunLoopRunInMode + 58 
    41 GraphicsServices     0x35d664ab GSEventRunModal + 114 
    42 GraphicsServices     0x35d66557 GSEventRun + 62 
    43 UIKit        0x338d5329 -[UIApplication _run] + 412 
    44 UIKit        0x338d2e93 UIApplicationMain + 670 
    45 TestApp        0x000029d7 main + 70 
    46 TestApp        0x00002958 start + 52 
) 
terminate called after throwing an instance of 'NSException' 

나는 또한> 사용 아래의 조건을 시도한 = 및 = 대신 문 사이의 < (아래 참조). 이 오류는 발생하지 않지만 모든 레코드를 가져 오지는 않습니다! 내가있는 NSDate 객체가 포함되지 않은 술어를 시도하고 잘 작동 시험으로

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(startTime >= %@) AND (startTime <= %@)", today, distantFuture]; 

(아래 참조) :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"location.title == %@", @"Boston"]; 

어떤 도움이나 조언이 많이 주시면 감사하겠습니다.

많은 감사합니다, 매튜


편집 당신의 도움 데이브에 대한

감사합니다.

이제는 <= 구조를 사용하고 있지만 개체가 반입되고 있습니다.

난 start3이 그 날짜 사이에 있다는 것을 확실히하기 위해 테스트로 todaydistantFuture을 사용했습니다. 실제 코드에서는 fromDate부터 toDate까지입니다. 여기

predicate: CAST(336261600.000000, "NSDate") <= startTime AND startTime <= CAST(339285599.000000, "NSDate") 
[sectionInfo numberOfObjects]: 0 

이 (속성 유형이 날짜로 설정) 코어 데이터에있는 기간은 다음과 같습니다 : 여기
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"((%@ <= %K) && (%K <= %@))", fromDate, @"startTime", @"startTime", toDate]; 

콘솔 출력입니다 : 여기

내 자 NSPredicate의

startTime 
--------- 
337249800 
337309200 
337318200 

왜 0 개 개체가 반환되는지 알 수 없습니다. 아무도 내가 잘못 가고있는 것을 볼 수 있습니까?

답변

4

아마도 사용하여 시도하고 대신 & & (콘솔 출력이 제대로 변환

난 그냥 나를 위해 작동 예를 게시 보여 보이지만, 참고 내 조건과 같이 대칭되어

@ "(%의 K> = %의 @) AND (% K = < % @)"

Here is the full example

+1

'AND'는 정확히 구문 분석됩니다. '&&'와 동일합니다. –

+0

http://zdam.posterous.com/bye.html – 7wonders

+0

불행히도 zdam.posterous.com 도메인의 링크는 작동하지 않습니다. – Krystian

24
the documentation에 따르면

, 집계 표현식으로 BETWEEN 작업자 수, ...

집계 식을 코어 데이터에서 지원되지 않습니다.

그래,이 문제를 해결하려면 >=<= 구조를 사용해야합니다.

그러나 날짜 중 하나가 [NSDate distantFuture] 인 경우 "미만"비교가 필요하지 않습니다.. 당신은 할 수 있어야한다 :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startTime >= %@", [NSDate date]]; 
+0

많은 분들께 감사드립니다. Dave. – mattvick

+3

그러나 여전히 내'> ='및'<='구성이 작동하지 않는 문제가 있습니다 :-( – mattvick

+0

SQLite 저장소에는 문제가있는 것 같지만 메모리 저장소는 다음 사이를 지원합니다. – Andy

관련 문제