다음 코드를 3 진 연산자를 사용하여 간단한 코드로 바꿀 수 있는지 알아 내려고합니다. 이 같은3 진 연산자로 If 문 바꾸기
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