在 Objective-C 中将 NSArray 转换为 NSString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1828665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert NSArray to NSString in Objective-C
提问by alexyorke
I am wondering how to convert an NSArray[@"Apple", @"Pear ", 323, @"Orange"]to a string in Objective-C.
我想知道如何在Objective-C中将NSArray转换[@"Apple", @"Pear ", 323, @"Orange"]为字符串。
回答by Dave DeLong
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
回答by Jason
One approach would be to iterate over the array, calling the descriptionmessage on each item:
一种方法是遍历数组,description在每个项目上调用消息:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[obj description]];
}
NSLog(@"The concatenated string is %@", result);
Another approach would be to do something based on each item's class:
另一种方法是根据每个项目的类做一些事情:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
if ([obj isKindOfClass:[NSNumber class]])
{
// append something
}
else
{
[result appendString:[obj description]];
}
}
NSLog(@"The concatenated string is %@", result);
If you want commas and other extraneous information, you can just do:
如果你想要逗号和其他无关信息,你可以这样做:
NSString * result = [array description];
回答by Pranay
I think Sanjay's answer was almost there but i used it this way
我认为 Sanjay 的回答差不多了,但我是这样用的
NSArray *myArray = [[NSArray alloc] initWithObjects:@"Hello",@"World", nil];
NSString *greeting = [myArray componentsJoinedByString:@" "];
NSLog(@"%@",greeting);
Output :
输出 :
2015-01-25 08:47:14.830 StringTest[11639:394302] Hello World
As Sanjay had hinted - I used method componentsJoinedByStringfrom NSArray that does joining and gives you back NSString
正如 Sanjay 所暗示的那样 - 我使用了 NSArray 中的componentsJoinedByString方法来加入并返回NSString
BTW NSStringhas reverse method componentsSeparatedByStringthat does the splitting and gives you NSArrayback .
顺便说一句,NSString具有反向方法componentsSeparatedByString进行拆分并返回NSArray。
回答by MarkP
I recently found a really good tutorial on Objective-C Strings:
我最近发现了一个关于 Objective-C 字符串的非常好的教程:
http://ios-blog.co.uk/tutorials/objective-c-strings-a-guide-for-beginners/
http://ios-blog.co.uk/tutorials/objective-c-strings-a-guide-for-beginners/
And I thought that this might be of interest:
我认为这可能很有趣:
If you want to split the string into an array use a method called componentsSeparatedByString to achieve this:
如果要将字符串拆分为数组,请使用称为 componentsSeparatedByString 的方法来实现此目的:
NSString *yourString = @"This is a test string";
NSArray *yourWords = [myString componentsSeparatedByString:@" "];
// yourWords is now: [@"This", @"is", @"a", @"test", @"string"]
if you need to split on a set of several different characters, use NSString's componentsSeparatedByCharactersInSet:
如果您需要拆分一组不同的字符,请使用 NSString 的 componentsSeparatedByCharactersInSet:
NSString *yourString = @"Foo-bar/iOS-Blog";
NSArray *yourWords = [myString componentsSeparatedByCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"-/"]
];
// yourWords is now: [@"Foo", @"bar", @"iOS", @"Blog"]
Note however that the separator string can't be blank. If you need to separate a string into its individual characters, just loop through the length of the string and convert each char into a new string:
但请注意,分隔符字符串不能为空。如果您需要将一个字符串分成单独的字符,只需遍历字符串的长度并将每个字符转换为一个新字符串:
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
NSString *ichar = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
[characters addObject:ichar];
}
回答by Sanjay Kakadiya
NSString * str = [componentsJoinedByString:@""];
and you have dic or multiple array then used bellow
并且您有 dic 或多个数组,然后在下面使用
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
回答by AbdulAziz Rustam Ogli
NSArray *array = [NSArray arrayWithObjects:@"One",@"Two",@"Three", nil];
NSString *stringFromArray = [array componentsJoinedByString:@" "];
The first line initializes an array with objects. The second line joins all elements of that array by adding the string inside the "" and returns a string.
第一行用对象初始化一个数组。第二行通过在 "" 内添加字符串来连接该数组的所有元素,并返回一个字符串。
回答by Asad Ali Choudhry
Objective C Solution
目标 C 解决方案
NSArray * array = @[@"1", @"2", @"3"];
NSString * stringFromArray = [[array valueForKey:@"description"] componentsJoinedByString:@"-"]; // "1-2-3"
Those who are looking for a solution in Swift
那些正在寻找解决方案的人 Swift
If the array contains strings, you can use the String's join method:
如果数组包含字符串,则可以使用 String 的 join 方法:
var array = ["1", "2", "3"]
let stringFromArray = "-".join(array) // "1-2-3"
In Swift 2:
在 Swift 2 中:
var array = ["1", "2", "3"]
let stringFromArray = array.joinWithSeparator("-") // "1-2-3"
In Swift 3 & 4
在斯威夫特 3 和 4 中
var array = ["1", "2", "3"]
let stringFromArray = array.joined(separator: "-") // "1-2-3"
回答by jasmo2
The way I know is easy.
我知道的方法很简单。
var NSArray_variable = NSArray_Object[n]
var stringVarible = NSArray_variable as String
nis the inner position in the array
This in SWIFT Language.
It might work in Objective C
n是数组中的内部位置 This 在 SWIFT 语言中。它可能适用于目标 C
回答by AhmedZah
Swift 3.0 solution:
Swift 3.0 解决方案:
let string = array.joined(separator: " ")

