Objective-c에서 배열에 가입하십시오
-
21-08-2019 - |
문제
나는 nsmutablearray를 문자열로 바꾸는 방법을 찾고 있습니다. 이 루비 배열 방법과 동등한 것이 있습니까?
>> array1 = [1, 2, 3]
>> array1.join(',')
=> "1,2,3"
건배!
해결책
NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:@","];
componentsJoinedByString:
지정된 문자열로 배열의 구성 요소를 결합하고 배열의 문자열 표현을 반환합니다.
다른 팁
당신이 찾고있는 방법은입니다 componentsJoinedByString
.
NSArray *a = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];//returns a pointer to NSArray
NSString *b = [a componentsJoinedByString:@","];//returns a pointer to NSString
NSLog(@"%@", b); // Will output 1,2,3
NSArray *pathArray = [NSArray arrayWithObjects:@"here",
@"be", @"dragons", nil];
NSLog(@"%@",
[pathArray componentsJoinedByString:@" "]);
제휴하지 않습니다 StackOverflow