Rejoindre un tableau en Objective-C
-
21-08-2019 - |
Question
Je cherche une méthode pour transformer un NSMutableArray en chaîne. Existe-t-il un équivalent de cette méthode de tableau Ruby?
>> array1 = [1, 2, 3]
>> array1.join(',')
=> "1,2,3"
Salut!
La solution
NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];
NSString *joinedString = [array1 componentsJoinedByString:@","];
componentsJoinedByString:
joindra les composants du tableau par la chaîne spécifiée et renverra une représentation sous forme de chaîne du tableau.
Autres conseils
La méthode que vous recherchez est 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:@" "]);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow