2014-06-23 2 views
0

다음 코드를 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을 반환합니다.

어쨌든이 코드를 줄일 수는 없지만 어쨌든이 코드를 많이 사용하기 때문에 시도해 볼 가치가 있습니다.

+0

"이 기능을 많이 사용하기 때문에"- 그렇다면 자체 기능/방법에 넣지 않으시겠습니까? – Kreiri

+2

줄이 작습니다! = 더 간단합니다 ... –

+0

물론 고맙습니다. 확실한 옵션이지만 문제는 개념적입니다.이 코드를 줄일 수 있는지 알고 싶습니다. 그리고 나를 위해, 더 적은 줄 == 더 간단합니다. :) – Jorge

답변

1

우선이 코드를 많이 사용하면 작은 도우미 기능을 사용해야합니다. 두 번째 문제는, 당신의 예는 누락 된 상태이며, 의미는 원하는 결과 전혀 다른 : 어떤 경우에 매우 읽을 수

self.notaLabel.text = 
[NSString stringWithFormat:@"%@/%@", 
    [self.pesoNota[@"nota"] floatValue] > 0.0 ? // condition 
    [nf stringFromNumber:[NSNumber numberWithFloat:[self.pesoNota[@"nota"] floatValue]]] // true case 
    : @"ND" // false case 
    , 
    [exercicio[@"notaComunidade"] floatValue] > 0.0 ? // condition 
    [nf stringFromNumber:[NSNumber numberWithFloat:[exercicio[@"notaComunidade"] floatValue]]] // true case 
    : @"ND" // false case 
]; 

:

self.notaLabel.text = 
[NSString stringWithFormat:@"%@/%@", 
    [nf stringFromNumber: 
     [NSNumber numberWithFloat:[self.pesoNota[@"nota"] floatValue]]] ? 
     /* missing condition */ : 
     @"ND", 

    [nf stringFromNumber: 
     [NSNumber numberWithFloat:[exercicio[@"notaComunidade"] floatValue]]] ? 
     /* missing condition */ : 
     @"ND" 
]; 

그것은 뭔가를해야합니다. 난 당신이 지저분한 코드를 방지하기 위해 바로 로컬 변수를 선언 할 수 있습니다 의미 : 그럼

- (NSString *)floatStringOrNDForNumber:(NSNumber *)number numberFormatter:(NSNumberFormatter *)numberFormatter { 
    return ([number floatValue] > 0.0f) ? [numberFormatter stringFromNumber:number] : @"ND"; 
} 

당신은 꽤 깨끗하고 이해할 수있는 몇 줄을 가지고

NSNumber* notaValue = self.pesoNota[@"nota"]; 
NSNumber* notaComunidade = exercicio[@"notaComunidade"]; 

self.notaLabel.text = [NSString stringWithFormat:@"%@/%@", 
    [notaValue floatValue] > 0 ? [nf stringFromNumber:notaValue] : @"ND", 
    [notaComunidade floatValue] > 0 ? [nf strungFromNumber:notacomunidadate] : @"ND" 
]; 
+0

정확히 내가 무엇을 찾고 있었습니까. 감사. – Jorge

1

코드는 다른 방법으로 추출 할 수있는 중복을 포함 코드 :

suaNota = [self floatStringOrNDForNumber:self.pesoNota[@"nota"] numberFormatter:nf]; 
notaComunidade = [self floatStringOrNDForNumber:exercicio[@"notaComunidade"] numberFormatter:nf]; 
self.notaLabel.text = [NSString stringWithFormat:@"%@/%@", suaNota, notaComunidade];