Jak dołączyć elementy NSArray do NSString?
Biorąc pod uwagę NSArray NSStrings, czy istnieje szybki sposób, aby połączyć je razem w jeden NSString (z separatorem)?
127
Author: Dave DeLong, 2009-09-07
3 answers
NSArray * stuff = /* ... */;
NSString * combinedStuff = [stuff componentsJoinedByString:@"separator"];
Jest to odwrotność -[NSString componentsSeparatedByString:]
.
315
Author: Dave DeLong,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-07 03:38:36
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-07 03:38:36
-componentsJoinedByString:
na NSArray powinno załatwić sprawę.
12
Author: BJ Homer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-07 03:38:43
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-09-07 03:38:43
Istnieje również ten wariant, jeśli oryginalna tablica zawiera obiekty Key-Value, z których chcesz wybrać tylko jedną właściwość (która może być serializowana jako łańcuch znaków):
@implementation NSArray (itertools)
-(NSMutableString *)stringByJoiningOnProperty:(NSString *)property separator:(NSString *)separator
{
NSMutableString *res = [@"" mutableCopy];
BOOL firstTime = YES;
for (NSObject *obj in self)
{
if (!firstTime) {
[res appendString:separator];
}
else{
firstTime = NO;
}
id val = [obj valueForKey:property];
if ([val isKindOfClass:[NSString class]])
{
[res appendString:val];
}
else
{
[res appendString:[val stringValue]];
}
}
return res;
}
@end
1
Author: Ben G,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-14 15:00:14
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-14 15:00:14