3

分析我的 ios 项目时出现此错误

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

这就是代码发生位置的样子。此方法用于对从数据库返回给我的数组进行排序。

- (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) {
//..
4

2 回答 2

5

编译器之所以抱怨,是因为它认为可以在有值==之前进行比较。result

给定您的代码的最佳选择是使用else ifand 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.
}
...

或者你可以这样做:

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];
...

甚至这样:

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

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

这样编译器就知道,如果达到比较,则 的值result已经被设置。

于 2012-10-11T20:42:13.593 回答
0

我认为这只是 llvm “求解器”不理解 <、> 或 == 中的一个对于制造商 ID [12] 比较必须是正确的。尝试删除该if (manufacturerID1 == manufacturerID2)块,以便它可以判断出result未初始化的值没有被使用。尽管您发表了评论,但您实际上并没有在那里初始化它的值。

于 2012-10-11T20:43:20.127 回答