我有一个 NSDate 格式的 NSArray 中的非连续日期数组。我需要数组中的相同日期集,但格式为 dd/MM 的 NSString。我想我需要循环读取原始数组中的每个日期,将其转换为字符串并根据需要对其进行格式化,但我不知道如何逐步遍历原始数组。
NSError *error2;
NSArray *swim750mArray = [context executeFetchRequest:request2 error:&error2];
if (swim750mArray == nil) {
NSLog(@"The fetch request2 returned an array == nil");
} else {
NSLog(@"The fetch request2 returned an array...");
NSLog(@"%@", swim750mArray);
NSArray *datesAsDates = [swim750mArray valueForKey:@"date"];
NSMutableArray* arrayOfDatesAsStrings = [NSMutableArray arrayWithCapacity:count];
//SomeCode - Convert the Array of NSDates to a new Array of dates as NSStrings...
for (int i=0; i<index; i++) {
//get the date next NSDate from the array as: NSDate *dateAsDate...
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM"];
NSString *dateAsStr = [dateFormat stringFromDate:dateAsDate];
[arrayOfDatesAsStrings addObject:dateAsStr];
}
这个答案接近我需要的,只是它不从现有数组中读取:How to use NSDate formatter with an array of dates?
请帮忙!预先非常感谢...
最佳答案
使用 fast enumeration 怎么样? ?
for (NSDate* dateAsDate in datesAsDates) {
//get the date next NSDate from the array as: NSDate *dateAsDate...
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM"];
NSString *dateAsStr = [dateFormat stringFromDate:dateAsDate];
[arrayOfDatesAsStrings addObject:dateAsStr];
}
关于objective-c - 将 NSDate 的 NSArray 转换为格式化日期字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13201325/