我想弄清楚是否有可能用简单的代码,使用三元运算符替换下面的代码。替换如果语句与三元运算符
if ([self.pesoNota[@"nota"] floatValue] > 0.0) {
suaNota = [nf stringFromNumber:[NSNumber numberWithFloat:[self.pesoNota[@"nota"] floatValue]]];
}
else {
suaNota = @"ND";
}
if ([exercicio[@"notaComunidade"] floatValue] > 0.0) {
notaComunidade = [nf stringFromNumber:[NSNumber numberWithFloat:[exercicio[@"notaComunidade"] floatValue]]];
}
else {
notaComunidade = @"ND";
}
self.notaLabel.text = [NSString stringWithFormat:@"%@/%@", suaNota, notaComunidade];
事情是这样的:
self.notaLabel.text = [NSString stringWithFormat:@"%@/%@", [nf stringFromNumber:[NSNumber numberWithFloat:[self.pesoNota[@"nota"] floatValue]]] ? : @"ND", [nf stringFromNumber:[NSNumber numberWithFloat:[exercicio[@"notaComunidade"] floatValue]]] ? : @"ND"];
第二个代码不给我预期的结果,它的情况下,返回0第一个表达式是假的,我希望它返回一个字符串。
无论如何,我不认为有可能减少此代码,但无论如何,这是值得一试,因为我使用了很多。
“因为我使用了很多” - 所以为什么不把它放入它自己的函数/方法? – Kreiri
更少的行!=更简单... –
非常感谢,这肯定是一个选项,但问题是更概念化,我只想知道是否有可能减少此代码。对我而言,更少的线==简单。 :) – Jorge