kart
kart

Reputation: 45

iOS Incompatible pointer types returning NSDictionary from NSMutableDictionary

I'm having an issue where I use NSMutableDictionaries returns values from NSDictionary.

Here is the warning message:

incompatible pointer types returning 'NSDictionary *' from a function with result type 'NSMutableDictionary *'

Here is the code:

- (NSMutableDictionary *)dictionaryWithContentsAtPath:(NSString *)path 

{

if ([path rangeOfString:@"/SessionStore"].location == 0) 
{

return [_inMemoryCache objectForKey:[path stringByReplacingCharactersInRange:NSMakeRange(0, 13) withString:@""]];

}

if ([path rangeOfString:@"/PermanentStore"].location == 0) 

{

return [NSDictionary dictionaryWithContentsOfFile:[self.persistentStoragePath stringByAppendingPathComponent:[path substringFromIndex:15]]];

}

return nil;

}

Please help me how to resolve this warning.

Upvotes: 2

Views: 2372

Answers (1)

Dave Wood
Dave Wood

Reputation: 13333

You need to ensure you're returning the right type. Your method states it's returning an NSMutableDictionary, but then returns only an NSDictionary.

Try this instead:

- (NSMutableDictionary*)dictionaryWithContentsAtPath:(NSString*)path {

    if ([path rangeOfString:@"/SessionStore"].location == 0) {
        return [[_inMemoryCache objectForKey:[path stringByReplacingCharactersInRange:NSMakeRange(0, 13) withString:@""]] mutableCopy];
    }

    if ([path rangeOfString:@"/PermanentStore"].location == 0) {
        return [NSMutableDictionary dictionaryWithContentsOfFile:[self.persistentStoragePath stringByAppendingPathComponent:[path substringFromIndex:15]]];
    }

    return nil;
}

Note: added a call to mutableCopy to turn your literal NSDictionary into a mutable version, and in the second case, used called the dictionaryWithContentsOfFile method on the NSMutableDictionary subclass instead of the NSDictionary parent class.

Upvotes: 1

Related Questions