我在分析我的ios项目时遇到此错误
The left operand of '==' is a garbage value
这就是代码出现的位置。此方法用于对从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) {
//..
答案 0 :(得分:5)
编译器抱怨,因为它认为在==
有值之前可以进行result
比较。
代码的最佳选择是使用else if
和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
的值已经设置。
答案 1 :(得分:0)
我认为只是llvm“求解器”才能理解,对于manufacturerID [12]比较而言,&lt;,&gt;或==中的一个必须是真的。尝试删除if (manufacturerID1 == manufacturerID2)
块,以便它可以告知未使用未初始化的result
的值。尽管你的评论,你实际上并没有初始化它的价值。