188

我想知道如何将NSArray转换为 Objective-C[@"Apple", @"Pear ", 323, @"Orange"]中的字符串。

4

9 回答 9

538
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
于 2009-12-01T20:31:08.633 回答
32

一种方法是遍历数组,description在每个项目上调用消息:

NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
    [result appendString:[obj description]];
}
NSLog(@"The concatenated string is %@", result);

另一种方法是根据每个项目的类做一些事情:

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);

如果你想要逗号和其他无关信息,你可以这样做:

NSString * result = [array description];
于 2009-12-01T20:28:35.720 回答
17

I think Sanjay's answer was almost there but i used it this way

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 componentsJoinedByString from NSArray that does joining and gives you back NSString

BTW NSString has reverse method componentsSeparatedByString that does the splitting and gives you NSArray back .

于 2015-01-25T16:54:35.693 回答
10

我最近发现了一个关于 Objective-C 字符串的非常好的教程:

http://ios-blog.co.uk/tutorials/objective-c-strings-a-guide-for-beginners/

我认为这可能很有趣:

如果要将字符串拆分为数组,请使用名为 componentsSeparatedByString 的方法来实现:

NSString *yourString = @"This is a test string";
    NSArray *yourWords = [myString componentsSeparatedByString:@" "];

    // yourWords is now: [@"This", @"is", @"a", @"test", @"string"]

如果你需要拆分一组不同的字符,使用 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];
}
于 2013-10-23T13:33:21.850 回答
9
NSString * str = [componentsJoinedByString:@""];

and you have dic or multiple array then used bellow

NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];   
于 2014-06-04T12:07:09.750 回答
2
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.

于 2017-09-21T13:31:29.957 回答
1

Objective C Solution

NSArray * array = @[@"1", @"2", @"3"];
NSString * stringFromArray = [[array valueForKey:@"description"] componentsJoinedByString:@"-"];   // "1-2-3"

Those who are looking for a solution in Swift

If the array contains strings, you can use the String's join method:

var array = ["1", "2", "3"]

let stringFromArray = "-".join(array) // "1-2-3"

In Swift 2:

var array = ["1", "2", "3"]

let stringFromArray = array.joinWithSeparator("-") // "1-2-3"

In Swift 3 & 4

var array = ["1", "2", "3"]

let stringFromArray = array.joined(separator: "-") // "1-2-3"
于 2019-06-09T08:39:03.323 回答
-2

The way I know is easy.

var NSArray_variable = NSArray_Object[n]
var stringVarible = NSArray_variable as String

n is the inner position in the array This in SWIFT Language. It might work in Objective C

于 2015-02-22T22:11:47.137 回答
-2

Swift 3.0 solution:

let string = array.joined(separator: " ")
于 2017-02-01T16:11:03.000 回答