我想知道如何将NSArray转换为 Objective-C[@"Apple", @"Pear ", 323, @"Orange"]
中的字符串。
9 回答
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
一种方法是遍历数组,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];
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 .
我最近发现了一个关于 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];
}
NSString * str = [componentsJoinedByString:@""];
and you have dic or multiple array then used bellow
NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
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.
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"
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
Swift 3.0 solution:
let string = array.joined(separator: " ")