Programing

Objective-C에서 배열 결합

lottogame 2020. 7. 3. 18:44
반응형

Objective-C에서 배열 결합


NSMutableArray를 문자열로 변환하는 방법을 찾고 있습니다. 이 Ruby 배열 메소드와 동등한 것이 있습니까?

>> 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:@" "]);

참고 URL : https://stackoverflow.com/questions/845622/join-an-array-in-objective-c

반응형