Pergunta

I am getting this error when I analize my ios project

The left operand of '==' is a garbage value

this is what the code looks like where its occurring.. This method is used to sort the array I have that is returned to me from the DB.

- (NSMutableArray *)startSortingTheArray:(NSMutableArray *)unsortedArray
{
    [unsortedArray sortUsingComparator:^ NSComparisonResult(SearchResultItem *d1, SearchResultItem *d2) {
        //initalize comparison
        NSComparisonResult result;

        NSInteger manufacturerID1 = d1.manufacturerID;
        NSInteger manufacturerID2 = d2.manufacturerID;
            if (manufacturerID1 > manufacturerID2)
                return NSOrderedAscending;
            if (manufacturerID1 < manufacturerID2)
                return NSOrderedDescending;
            if (manufacturerID1 == manufacturerID2) {

            NSString *model1 = d1.model;
            NSString *model2 = d2.model;
            result = [model1 localizedCompare:model2];
        }
        if (result == NSOrderedSame) {
//..
Foi útil?

Solução

The compiler is complaining because it believes it's possible to reach the == comparison before result has a value.

The best option given your code is to use else if and else:

if (manufacturerID1 > manufacturerID2)
    return NSOrderedAscending; // we won't reach the comparison so result doesn't matter
else if (manufacturerID1 < manufacturerID2)
    return NSOrderedDescending; // we won't reach the comparison so result doesn't matter
else {
    NSString *model1 = d1.model;
    NSString *model2 = d2.model;
    result = [model1 localizedCompare:model2]; // in any other case, result will be set.
}
...

Or you could do this:

NSComparisonResult result;
...
if (manufacturerID1 > manufacturerID2)
    return NSOrderedAscending;
else if (manufacturerID1 < manufacturerID2)
    return NSOrderedDescending;

NSString *model1 = d1.model;
NSString *model2 = d2.model;
result = [model1 localizedCompare:model2];
...

Or even this:

if (manufacturerID1 > manufacturerID2)
    return NSOrderedAscending;
else if (manufacturerID1 < manufacturerID2)
    return NSOrderedDescending;

NSComparisonResult result = [d1.model localizedCompare:d2.model];
...

This way the compiler knows that if the comparison is reached, the value of result will already have been set.

Outras dicas

I think it's just the llvm “solver” not understanding that precisely one of <, >, or == must be true in relation to the manufacturerID[12] comparisons. Try removing the if (manufacturerID1 == manufacturerID2) block, so that it can tell that the value of result isn't being used uninitialized. Despite your comment, you don't actually initialize its value, there.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top