views:

89

answers:

1

Hello,

I am developing an iPhone app and are using xCode to do "Build and analyze". I get the message "Receiver in expresseion is a garbage value" pointing to the line of the return-row in my bode below:

-(UIColor*)getLevelColor{
UIColor* tempCol;

if (level==4) {
    tempCol= [[UIColor alloc] initWithRed:0.39f green:0.82f blue:0.32f alpha:1.0f];
}else if (level==5) {
    tempCol= [[UIColor alloc] initWithRed:0.61f green:0.68f blue:0.83f alpha:1.0f];
}else if (level==6) {
    tempCol= [[UIColor alloc] initWithRed:0.90f green:0.68f blue:0.99f alpha:1.0f];
}else if (level==7) {
    tempCol= [[UIColor alloc] initWithRed:0.68f green:0.97f blue:0.99f alpha:1.0f];
}

return [tempCol autorelease];
}

How come I get this and how do I fix it so I don't get the message?

Thanks in advance!

Niklas

+3  A: 

I'd guess that the compiler sees that there is a code path which returns an uninitialized value and gives you the news: tempCol is an uninitialized value unless level is anything else than 4,5,6 or 7. If you are sure that level will never be anything than 4,5,6 or 7 then add a else { return 0; } to the end of your if-ladder.

PS: You should use a switch(){} statement if you have to compare against a series of constant values, this is often faster and easier to maintain (IMHO). Alternatively, you could place the RGBA values in an array which you index with level.

(Disclaimer: I don't do Objective-C. Nobody should do it)

Luther Blissett
Thanks for your answer. I think that creating an array and indexing with levels is a nice solution and it solves the problem if the reason is as you explained, which it should be. P.S. Well, if you want to develop for iPhone, I guess Objective-C is the only option D.S. /Niklas (normally a Java-guy...).
Nicsoft