gpt4 book ai didi

ios - 如何将迭代的字符串值结果集转换为ios中的数组?

转载 作者:行者123 更新时间:2023-11-29 12:06:11 25 4
gpt4 key购买 nike

我希望得到如下结果:

set arr[0]=jan23,2016 …… upto arr[6] =jan29,2016.

为此,我有以下代码:

// To get future or next 6 days from now or current date
NSCalendar * calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents * datecomponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDayfromDate:[NSDate date]];
for(int i=0;i<6;++i)
{
NSDate * todaydate = [calendar dateFromComponents:datecomponents];
NSLog(@"%@",todaydate);
++datecomponents.day;
NSDateFormatter * dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterMediumStyle];
NSString * convertedstring = [dateformatter stringFromDate:todaydate];
NSLog(@“converteddate:%@",convertedstring);

为此,我收到了如下回复:

converteddate:Jan 23, 2016 similarly upto jan29,2016

然后我这样做:

NSMutableString * newstring = [[NSMutableString alloc]init];
[newstring stringByAppendingString:convertedstring];
NSLog(@"%@",newstring);

还有这样的:

    NSMutableArray * arrdata;
arrdata = [[NSMutableArray alloc]init];
[arrdata addObject:convertedstring];
NSLog(@"%@",arrdata);
}

如何得到上面的结果集。字符串迭代后如何将它们转换成数组。

最佳答案

数组必须在 for 循环之外声明和分配,因为您每次迭代都会重置它:

NSMutableArray * arrdata = [[NSMutableArray alloc]init];
NSCalendar * calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents * datecomponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDayfromDate:[NSDate date]];
for(int i=0;i<6;++i)

也没有必要这样做,因为你永远不会真正使用 newstring :

NSMutableString * newstring = [[NSMutableString alloc]init];
[newstring stringByAppendingString:convertedstring];

关于ios - 如何将迭代的字符串值结果集转换为ios中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34941715/

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