Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some getters do not return nil when they have not been set #75

Closed
jacobkeeler opened this issue Feb 10, 2015 · 1 comment
Closed

Some getters do not return nil when they have not been set #75

jacobkeeler opened this issue Feb 10, 2015 · 1 comment
Labels
bug A defect in the library

Comments

@jacobkeeler
Copy link

For each of the non-enum RPC classes, there will sometimes be one or more properties of a type that is a subclass of SDLRPCStruct (e.g. SDLImage). If any of the getters for these properties are called when the property has not been set, then, instead of returning nil, the getter will return an object of the appropriate type initialized with a dictionary of nil.

Example:

-(SDLImage*) image {
    NSObject* obj = [store objectForKey:NAMES_image];
    if ([obj isKindOfClass:SDLImage.class]) {
        return (SDLImage*)obj;
    } else {
        return [[SDLImage alloc] initWithDictionary:(NSMutableDictionary*)obj];
    }
}

If image was not previously set, then obj will equal nil. Because of this, the second return statement will run, and it will return [[SDLImage alloc] initWithDictionary:nil].

As a result of the nil dictionary, if the user tries to edit this object directly from the getter reference in any way, nothing will happen. The user will have no way of knowing this, as they cannot check whether the internal dictionary is nil. This would result in some very hard to diagnose errors.

Example:

SDLChoice* choice = [[SDLChoice alloc] init];
SDLImage* image = choice.image;
if (image != nil) {
    image.imageType = [SDLImageType STATIC];
    NSLog(@"%@", image.imageType); //"nil"
}

To fix this, a obj == nil check could be added to each of these getters as such:

-(SDLImage*) image {
    NSObject* obj = [store objectForKey:NAMES_image];
    if (obj == nil || [obj isKindOfClass:SDLImage.class]) {
        return (SDLImage*)obj;
    } else {
        return [[SDLImage alloc] initWithDictionary:(NSMutableDictionary*)obj];
    }
}
@justinjdickow
Copy link
Contributor

@adein @asm09fsu @joeljfischer

We would like to review this issue with you guys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A defect in the library
Projects
None yet
Development

No branches or pull requests

3 participants