EZForm icon indicating copy to clipboard operation
EZForm copied to clipboard

EZFormRadioField.valueOrUnselectedAtIndex throws NSInvalidArgumentException

Open orkenstein opened this issue 9 years ago • 1 comments

This part of code:

- (NSString *)valueOrUnselectedAtIndex:(NSUInteger)index
{
    NSString *value = nil;
    if (self.unselected) {
    if (index == 0) {
        value = self.unselected;
    }
    else {
        NSString *key = [self.orderedKeys objectAtIndex:index - 1];
        value = [self.choices valueForKey:key];
    }
    }
    else {
    NSString *key = [self.orderedKeys objectAtIndex:index];
    value = [self.choices valueForKey:key];
    }
    return value;
}

... should use -(id)objectForKey instead of -(id)valueForKey. Otherwise, if u use for example NSNumber as keys, u get:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
 reason: '-[__NSCFNumber length]: unrecognized selector sent to instance

orkenstein avatar Aug 12 '14 10:08 orkenstein

Using valueForKey: with NSDictionary requires a string parameter as per the NSDictionary docs:

"Note that when using key-value coding, the key must be a string."

The EZForm code you've highlighted is also explicitly using NSStrings when fetching the key from orderedKeys which is correct use of the API.

NSDictionary also defers to objectForKey: in most uses of valueForKey:

Discussion If key does not start with “@”, invokes objectForKey:. If key does start with “@”, strips the “@” and invokes [super valueForKey:] with the rest of the key.

I'm in two minds about whether or not this is an issue. I can't see a lot of use cases for NSNumber keys in a form, but having said that you could easily rewrite that method to read self.choices[key] and it probably wouldn't cause any harm.

jessedc avatar Aug 17 '14 18:08 jessedc