gpt4 book ai didi

iOS 如何将逗号分隔的 Array 对象转换为字符串?

转载 作者:行者123 更新时间:2023-11-29 02:05:45 28 4
gpt4 key购买 nike

我有一个名为 child_list 的数组,例如 child_list: "9,8,21,22,20,24,25",将其放入索引 0 处的 NSString 中当我将它转换为字符串时它可以工作,但是当我使用 NSMutableArray 时它崩溃了,我不知道为什么。

我的代码在这里:

if([theDictionary objectForKey:@"child_list"])
{
NSString *str = [[theDictionary objectForKey:@"child_list"]objectAtIndex:0];
NSLog(@"Child List %@",str);
_child_list = [[NSMutableArray alloc ]initWithObjects: [str componentsSeparatedByString:@","], nil];
}

当断点出现在 str 上时,它显示结果;当断点出现在 _child_list 上时,它会崩溃。

最佳答案

分配_child_list NSMutableArray时使用initWithArray方法

 if([theDictionary objectForKey:@"child_list"])
{
NSString *str = [[theDictionary objectForKey:@"child_list"]objectAtIndex:0];
NSLog(@"Child List %@",str);
_child_list = [[NSMutableArray alloc ] initWithArray:[str componentsSeparatedByString:@","]];
}

关于iOS 如何将逗号分隔的 Array 对象转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813858/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com