Search code examples
objective-cnsdatenspredicatexlform

XLForm predicate for NSDate comparison


I'm using the inline datePicker to select a date (dd/mm/yyyy). It's a birthdate, and I have to show a form row if the date is less than [NSDate date] minus 18 years.

And I'm totally struggling with the predicates for show or hiding the row :x

Here is the code I use to setup the row I need to hide if the user is less than 18 years old

XLFormRowDescriptor *row = [XLFormRowDescriptor formRowDescriptorWithTag:kAuthorization rowType:MyXLFormDescriptionTypeCheck title:@"Parents authorization"]];

row.hidden = [NSString stringWithFormat:@"$%@.value >= CAST(%f, 'NSDate')", kDateOfBirth, [eighteenYearsAgo timeIntervalSince1970]];

[section addFormRow:row];

When I run my application, a breakpoint stops the execution at XLFormRowDescriptor.m (line 342)

where : self is the actual row, _hidden is equal to "$birthday.value >= CAST(881190000.000000, "NSDate")" and self.sectionDescriptor.formDescriptor.allRowsByTag is nil because self.sectionDescriptor is nil too

And here is the NSException's description in the @catch block :

Can't get value for 'birthday' in bindings {}.

Thank you for your help \o


Solution

  • I found a solution that do the job, by bypassing the problem.

    I overrided the method

    - [id<XLFormDescriptorDelegate> formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)formRow oldValue:(id)oldValue newValue:(id)newValue];
    

    So that I'm getting notified when the row's value has changed. I just do the comparison here and hide or show the row I want depending on the result.

    If someone is stuck on the same problem, I think this solution is great, not time expensive to setup (no NSPredicate nor regex skills required, and just one method to override), and not very dirty (code is readable, and solution is not a big trick) :)