开发者

Creation of NSString inside if-case fails?

I'm simply trying to decide which text I'm going with in a simple if-manner. Why does X-code complain and not let me build the project saying that the variable开发者_如何转开发 is undefined when it's clearly defined in the if?

 if(indexPath.row == [listOfItems count]) {
            NSString *cellValue = [[NSString alloc] initWithFormat:@"Totalt: %@st registrerade", companyTotalReg]; //cellValue is unused
        } else {
            NSString *cellValue = [[NSString alloc] initWithFormat:@"Totalt: SEK%@ intjänat", companyTotalPay]; //cellValue is unused
        }
        cell.textLabel.text = cellValue; //cellValue is undefined


You need to do it this way:

NSString *cellValue = nil;
if(indexPath.row == [listOfItems count]) {
    cellValue = [[NSString alloc] initWithFormat:@"Totalt: %@st registrerade", companyTotalReg]; //cellValue is unused
} else {
    cellValue = [[NSString alloc] initWithFormat:@"Totalt: SEK%@ intjänat", companyTotalPay]; //cellValue is unused
}
cell.textLabel.text = cellValue; //cellValue is not undefined anymore

Otherwise it could be that theoretically both your if clauses fail (even though this is not possible in your case) and cellValue remains undeclared. As the compiler cannot know if it is theoretically possible to have all your conditions fail, so it will just warn you anyway.

In general you should/must always initialize variables within the scope in which they are about to be used. In your case cellValue would be out of scope for cell.textLabel.text = cellValue;.

Kinda off topic, but you should also use NSLocalizedString() for any UI string, instead of hard coded ones.


 NSString *cellValue = NULL; 

if(indexPath.row == [listOfItems count]) {
    cellValue = [[NSString alloc] initWithFormat:@"Totalt: %@st registrerade", companyTotalReg]; //cellValue is unused
} else {
    cellValue = [[NSString alloc] initWithFormat:@"Totalt: SEK%@ intjänat", companyTotalPay]; //cellValue is unused
}
cell.textLabel.text = [cellValue autorelease]; // The autorelease is here because you are leaking the memory otherwise. If you release the string later anyway, you can and should remove it!


The way you did it defines two NSStrings which are both named cellValue. The lifetime of the first cellValue is limited to the scope of the if clause and the lifetime of the second cellValue is limited to the scope of the else clause. You could resolve this in two ways:

NSString *cellValue;
if (indexPath.row == [listOfItems count])
    cellValue = [[NSString alloc] initWithFormat:@"Totalt: %@st registrerade", companyTotalReg];
else
    cellValue = [[NSString alloc] initWithFormat:@"Totalt: SEK%@ intjänat", companyTotalPay]; 
cell.textLabel.text = cellValue;

or

NSString *cellValue = (indexPath.row == [listOfItems count])
    ? [[NSString alloc] initWithFormat:@"Totalt: %@st registrerade", companyTotalReg]
    : [[NSString alloc] initWithFormat:@"Totalt: SEK%@ intjänat", companyTotalPay];
cell.textLabel.text = cellValue;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜