NSString *str = nil;
NSString *a = [NSString stringWithString: str]; // crash
NSString *b = [NSString stringWithFormat: @"%@", str]; // null
삽질을 거치다 알게 된건데,
a 와 b 문자열의 경우 차이가 없어보이지만...
아래처럼 nil (NULL) 인 경우에 차이가 생긴다.
NSString *str = @"asdfasdf";
NSString *a = [NSString stringWithString: str];
NSString *b = [NSString stringWithFormat: @"%@", str];
하드코딩한 값이 아니라 서버에서 내려오는 값인 경우 nil 체크를 따로 거치기 귀찮다면 stringWithFormat 을 쓰는 것이 안전하다.
'iOS > 삽질' 카테고리의 다른 글
맥북 nvm use 이후에도 node 버전 변경 안됨. (0) | 2024.10.04 |
---|