问题
In my app using coredata i have an entity with two attributes GridValue(numerical value), RecordingDate(NSdate)). The user in one tableview can enter these two values each day and they are recorded with a fetch controller. For more understanding here is a screenshot of the two views concerned with the issue:
Th Addviewcontroller is where the user adds a new value and when user clicks on any of the two fields in the static tableview, he is redirected to the "Zahl Datum" view for editing.
What I would like to do is to check the GridValue entered by the user and display a warning(could be an alert message or in this case the footer message that would show or pop up whenever the new entered value is less than a previous value entered in a different day(a previous stored value of RecordingDate(the NSdate input attribute from CoreData model).
Example to illustrate if the current user enters: Zahlerstand = 1254 and Datum: 12,05,2013, and in a previous record the values were: Zahlerstand = 1300 and Datum: 11.05.2013. then we see than new value is smaller than old value on successive dates. In this case a warning should be displayed(as in the tableview section footer. before he can click on "save" button in the addviewcontroller.
Please help with some sample code if possible.
Thank you in advance,
回答1:
Use NSComparisonResult for comparing older date to newer date as :
compare: method returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.
- (NSComparisonResult)compare:(NSDate *)anotherDate
switch ([olderDate compare:newDate]) {
case NSOrderedAscending:
// olderDate is earlier in time than newDate
break;
case NSOrderedSame:
// Both dates are same
break;
case NSOrderedDescending:
// olderDate is later in time than newDate
break;
}
来源:https://stackoverflow.com/questions/17742725/ios-how-to-use-predicates-nsexpression-to-check-user-input-for-validity-and-con