삼항연산자 사용하기

posted on 06 Jul 2017 under category iOS

int a = 5;
NSString *b = @"b";
NSString *c = @"c";
NSString *result;
if( a > 10){
    result = b;
}else{
    result = c;
}

NSLog(@"%@", result);

위에 if-else 문을 간편하게 사용할수있다.

사용방법

조건 ? A : B

조건이 참이면 A를 반환하고 거짓이면 B를 반환한다.

그럼 위의 코드를 삼항연산자를 사용하면

int a = 5;
NSString *b = @"b";
NSString *c = @"c";
NSString *result;

result = a > 10 ? b : c;

로 코드를 줄일수있다.

Written by Brody Byun
Please Do not steal content from our website.
Please let me know if you want to take the post.(email or reply)