문제

나는 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 수업 참조:

NSArray *pathArray = [NSArray arrayWithObjects:@"here",
    @"be", @"dragons", nil];
NSLog(@"%@",
    [pathArray componentsJoinedByString:@" "]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top