Ios - [NSArray componentsJoinedByString] with a condition

I want to use -[NSArray componentsJoinedByString] with some condition, for example: to concatenate all elements of my array with ",", except for the last where I want "and".

This is python, it will be something like:

 ', '.join(array[:-1]) + ' and ' + array[-1] 

Is there a way or method that would do the trick on one line, avoiding all things if else ?

+4
source share
4 answers

You can use subarrayWithRange: and stringWithFormat: to do the same. You need at least 1 if to check the number of elements in the array and make sure that you have no index exception.

+3
source

try this, I don’t know if it is effective or not, but check once,

  NSArray *arr= [[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil]; NSString *string = [arr componentsJoinedByString:@","]; NSString *str= [NSString stringWithFormat:@"%@ and %@",[string substringToIndex:[string length]-[[arr lastObject] length]-1],[arr lastObject]]; NSLog(@"%@",str); 
+2
source

It may not be the most efficient, but it is a simple solution for making an array of objects that have string representations and turn them into a comma-separated list, for example, 1, 2, 3, and 4.

 @implementation NSArray (MyCollection) - (NSString *)stringCommaAndSeparated { return [self stringCommaAndSeparatedUsingStringConverter:^NSString *(id object) { assert( [object isKindOfClass:NSString.class] ); return (NSString *)object; }]; } - (NSString *)stringCommaAndSeparatedUsingStringConverter:(NSString *(^)(id object))stringConverter { assert( stringConverter ); if( self.count == 0 ) return @""; if( self.count == 1 ) return stringConverter(self.firstObject); if( self.count == 2 ) return [NSString stringWithFormat:NSLocalizedString(@"%@ and %@", nil), stringConverter(self[0]), stringConverter(self[1]) ]; NSMutableString *string = [[NSMutableString alloc] init]; for( int index = 0; index < self.count-1; index += 1 ) [string appendFormat:NSLocalizedString(@"%@, ", nil), stringConverter(self[index])]; [string appendFormat:NSLocalizedString(@"and %@", nil), stringConverter(self.lastObject)]; return string; // This is returning a mutable string, but we could copy it to an immutable one! } @end 

Here is an example of this in action:

  // Results in @"1, 2, 3, and 4" NSString *str = [@[@"1", @"2", @"3", @"4"] stringCommaAndSeparated]; // Results in @"1, 2, 3, and 4" NSString *str2 = [@[@(1), @(2), @(3), @(4)] stringCommaAndSeparatedUsingStringConverter:^NSString *(id object) { assert( [object isKindOfClass:NSNumber.class] ); NSNumber *number = object; return number.stringValue; }]; 
+1
source

Here is my category solution based on Wain's answer:

 @implementation NSArray (NSString) - (NSString *)listStringUsingLocale:(NSLocale *)locale { if(!self.count) return nil; if(!locale) locale = [NSLocale currentLocale]; NSString *language = [locale objectForKey: NSLocaleLanguageCode]; if([language isEqualToString:@"en"]) { if (self.count > 1) { NSArray *arr1 = [self subarrayWithRange:NSMakeRange(0, self.count - 1)]; return [NSString stringWithFormat:@"%@ and %@", [arr1 componentsJoinedByString:@", "], self.lastObject]; } else { return self[0]; } } return nil; } @end 

I added an option to add language settings.

0
source

Source: https://habr.com/ru/post/1492061/


All Articles