THIS IS ELLIE

문자열 포맷 지정 본문

개발/Objective-C

문자열 포맷 지정

Ellie Kim 2020. 4. 22. 16:59

Objective-C에서 문자열은 NSString클래스의 인스턴스이다.

문자열을 생성하는 방법은 클래스 메서드를 사용해 자동을 소멸하는 인스턴스를 생성하거나,

alloc 메서드를 사용해 인스턴스를 생성한 후 이니셜 라이져로 초기화하는 방법이 있다.

 

그중에서도 포맷을 지정해서 문자열을 생성하는 방법이다.

stringWithFormat 메서드 활용하고,

인스턴스를 만들고 싶을 때는 alloc메서드를 생성한 뒤 initWithFormat: 메서드를 사용하면 된다.

 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        NSString *str;
        int i = 10;
        
        str = [NSString stringWithFormat:@"int를 출력해보자 %d 이렇게 해도 OK", i];
        NSLog(@"%@", str);
        
        str = [[NSString alloc] initWithFormat:@"int를 출력해보자 %d 이렇게 해도 OK임",i];
        NSLog(@"%@", str);
    }
    return 0;
}

int를 출력해보자 10 이렇게 해도 OK

int를 출력해보자 10 이렇게 해도 OK임

이 잘 출력된다.

 

https://hyerios.tistory.com/101?category=853360

 

오브젝티브씨 문자열 포맷팅

Format Specifiers The format specifiers supprted by the NSString formatting methods and CFString formatting functions follow the IEEE printf speicification. %@ 오브젝티브씨 오브젝트a %d, %D 부호 있..

hyerios.tistory.com

 

반응형

'개발 > Objective-C' 카테고리의 다른 글

오브젝티브씨 isKindOfClass  (0) 2020.04.06
Objective-C 프로퍼티 속성  (0) 2020.03.26
KVO (Key Value Observing)  (0) 2020.02.24
Objective-C id 타입  (0) 2020.02.20
조건문와 반복문  (0) 2020.02.12