表达式中的接收者是垃圾值
我正在开发一个 iPhone 应用程序,并使用 xCode 进行“构建和分析”。我收到消息“表达式中的接收器是垃圾值”,该消息指向下面我的 bode 中返回行的行:
-(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];
}
我怎么会得到这个以及如何修复它,这样我就不会收到该消息?
提前致谢!
尼克拉斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜编译器会发现有一个代码路径返回未初始化的值,并给出消息:
tempCol
是一个未初始化的值,除非level
是除4、5、6 或 7。如果您确定level
永远不会超过 4、5、6 或 7,则添加else { return 0; }
到 if-ladder 的末尾。PS:如果您必须与一系列常量值进行比较,您应该使用
switch(){}
语句,这通常更快且更易于维护(恕我直言)。或者,您可以将 RGBA 值放入使用level
索引的数组中。(免责声明:我不做 Objective-C。没有人应该做)
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 unlesslevel
is anything else than 4,5,6 or 7. If you are sure thatlevel
will never be anything than 4,5,6 or 7 then add aelse { 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 withlevel
.(Disclaimer: I don't do Objective-C. Nobody should do it)