代码之家  ›  专栏  ›  技术社区  ›  alexyorke

在objective-c中将nsarray转换为nsstring

  •  180
  • alexyorke  · 技术社区  · 15 年前

    我想知道如何转换 不可变数组 [@"Apple", @"Pear ", 323, @"Orange"] 中的字符串 Objtovi-C .

    8 回复  |  直到 6 年前
        1
  •  528
  •   alexyorke    11 年前
    NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];
    
        2
  •  32
  •   Jason    15 年前

    一种方法是迭代数组,调用 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];
    
        3
  •  14
  •   Pranay    10 年前

    我想桑杰的答案就在那里,但我是这样用的

    NSArray *myArray = [[NSArray alloc] initWithObjects:@"Hello",@"World", nil];
    NSString *greeting = [myArray componentsJoinedByString:@" "];
    NSLog(@"%@",greeting);
    

    输出:

    2015-01-25 08:47:14.830 StringTest[11639:394302] Hello World
    

    正如桑杰暗示的那样-我用的方法 组件由字符串连接 从NSARRAY那里加入并把你送回 非字符串

    顺便说一句 非字符串 具有反向方法 由字符串分隔的组件 这样做会让你分心 不可变数组 回来。

        4
  •  10
  •   MarkP    11 年前

    我最近发现了一个关于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"]
    

    但是请注意分隔符字符串不能为空。如果需要将一个字符串分隔成单独的字符,只需循环遍历该字符串的长度,并将每个字符转换为一个新的字符串:

    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];
    }
    
        5
  •  8
  •   Sangram Shivankar Wostein    9 年前
    NSString * str = [componentsJoinedByString:@""];
    

    您有dic或多个数组,然后使用下面的

    NSString * result = [[array valueForKey:@"description"] componentsJoinedByString:@""];   
    
        6
  •  1
  •   AbdulAziz Rustam Ogli    7 年前
    NSArray *array = [NSArray arrayWithObjects:@"One",@"Two",@"Three", nil];
    NSString *stringFromArray = [array componentsJoinedByString:@" "];
    

    第一行用对象初始化数组。 第二行通过在“”中添加字符串来联接该数组的所有元素,并返回一个字符串。

        7
  •  -1
  •   erhun    10 年前

    我知道的方式很简单。

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

    n 是数组中的内部位置 这是迅速的语言。 它可能在 目标C

        8
  •  -1
  •   AhmedZah    6 年前

    Swift 3.0解决方案:

    let string = array.joined(separator: " ")