Question

I am getting this error from static analyzer :

This is the code :

API Misuse (Apple) - Dictionary cannot be nil

 - (NSString *)description
{
    return [@{@"filePath": self.filePath,
        @"fileName": self.fileName,
        @"fileAttributes": self.fileAttributes,
        @"creationDate": self.creationDate,
        @"modificationDate": self.modificationDate,
        @"fileSize": @(self.fileSize),
        @"age": @(self.age),
        @"isArchived": @(self.isArchived)} description];
}

Can any one tell what is the problem ?

Was it helpful?

Solution 2

Either one of your values is guaranteed to be nil, or one of your values is not an object pointer, because the following code yields no error at all:

- (NSString *)description
{
    return [@{@"filePath": @"",
          @"fileName": @"",
          @"fileAttributes": @"",
          @"creationDate": @"",
          @"modificationDate": @"",
          @"fileSize": @"",
          @"age": @"",
          @"isArchived": @""} description];
}

OTHER TIPS

That chunk of code looks like it's from a older version of Lumberjack's DDFileLogger.m

They got rid of the warning by inserting default empty strings if nil for items in the dictionary -> https://github.com/CocoaLumberjack/CocoaLumberjack/pull/127/files

    return [@{@"filePath": (self.filePath ?: @""),
          @"fileName": (self.fileName ?: @""),
          @"fileAttributes": (self.fileAttributes ?: @""),
          @"creationDate": (self.creationDate ?: @""),
          @"modificationDate": (self.modificationDate ?: @""),
          @"fileSize": @(self.fileSize),
          @"age": @(self.age),
          @"isArchived": @(self.isArchived)} description];

This can be caused by the implementation of a property's getter such us:

- (id)myLazyPropery
{
    if (featureDisabled) return nil;

    // ...

    return _myLazyPropery;
}

Than any of the following will trigger an API misuse warning:

@{self.myLazyProperty}
@[self.myLazyProperty]
[myMutableArray addObject:self.myLazyProperty]

This can be fixed by using methods that accept nil values such us arrayWithObject:.

Too bad the Analyzer doesn't point the getter as the source of the problem.

Also it won't silence with if (self.myProperty){@[self.myLazyProperty]} or self.myProperty ? @[self.myProperty] : @[], which I think is a bug.

The analyizer is saying that it's possible one of your dictionary items values could be nil. Simply check if(!someValue) {// handle nil here, by adding a null object, empty string, returning, etc. } and this will quiet the warning.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top