如何获取 NSMutableArray 特定对象
Posted
技术标签:
【中文标题】如何获取 NSMutableArray 特定对象【英文标题】:How to get NSMutableArray specific objects 【发布时间】:2016-12-07 11:53:07 【问题描述】:我想在周日和周一文本对象下面的这个数组列表中获取并保存在另一个数组中。换句话说,获取带有日期和工作日名称的对象都可以获取并保存在数组中
请帮助我该怎么做。我很努力,但没有得到正确的答案。
NSDateComponents *components;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *startDate = [dateFormat dateFromString:@"25-11-2016"];
NSDate *endDate = [dateFormat dateFromString:@"7-12-2016"];
[dateFormat setDateFormat:@"dd-MM-yyyy, EEEE"];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];
int days = [components day];
for (int x = 0; x <= days; x++)
[mutable_ary addObject:[dateFormat stringFromDate:startDate]];
NSLog(@"%@",[dateFormat stringFromDate:startDate]);
startDate = [startDate dateByAddingTimeInterval:(60 * 60 * 24)];
NSLog(@"%@",mutable_ary);
"25-11-2016, Friday",
"26-11-2016, Saturday",
"27-11-2016, Sunday",
"28-11-2016, Monday",
"29-11-2016, Tuesday",
"30-11-2016, Wednesday",
"01-12-2016, Thursday",
"02-12-2016, Friday",
"03-12-2016, Saturday",
"04-12-2016, Sunday",
"05-12-2016, Monday",
"06-12-2016, Tuesday",
"07-12-2016, Wednesday"
输出:
"27-11-2016, Sunday",
"28-11-2016, Monday",
"04-12-2016, Sunday",
"05-12-2016, Monday",
【问题讨论】:
让我们看看你到目前为止有什么。 @ElTomato 请检查我更新的问题 请发布您的代码。 @shallowThought 请检查更新的问题 请同时发布预期结果。我不太明白你想要达到什么目的。 【参考方案1】:试试这个:
NSMutableArray *arrList = [[NSMutableArray alloc]initWithObjects:@"25-11-2016, Friday",
@"26-11-2016, Saturday",
@"27-11-2016, Sunday",
@"28-11-2016, Monday",
@"29-11-2016, Tuesday",
@"30-11-2016, Wednesday",
@"01-12-2016, Thursday",
@"02-12-2016, Friday",
@"03-12-2016, Saturday",
@"04-12-2016, Sunday",
@"05-12-2016, Monday",
@"06-12-2016, Tuesday",
@"07-12-2016, Wednesday", nil];
NSMutableArray *result = [[NSMutableArray alloc]init];
for (id element in arrList)
NSString *str = element;
if ([str containsString:@"Monday"] || [str containsString:@"Sunday"])
[result addObject:element];
// print result
NSLog(@"%@",result);
【讨论】:
感谢您宝贵的时间。非常感谢,您的帮助解决了我的问题。我得到具体的日子。非常感谢你:) 你在吗?请解决我与此问题相关的另一个问题。【参考方案2】:NSArray *input = @[@"25-11-2016, Friday",
@"26-11-2016, Saturday",
@"27-11-2016, Sunday",
@"28-11-2016, Monday",
@"29-11-2016, Tuesday",
@"30-11-2016, Wednesday",
@"01-12-2016, Thursday",
@"02-12-2016, Friday",
@"03-12-2016, Saturday",
@"04-12-2016, Sunday",
@"05-12-2016, Monday",
@"06-12-2016, Tuesday",
@"07-12-2016, Wednesday"];
NSMutableArray *days = [NSMutableArray arrayWithArray: @[@"Sunday",@"Monday"]];
NSMutableArray *output = [NSMutableArray new];
for(NSString *day in days)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@",day];
NSArray *filtered = [input filteredArrayUsingPredicate:predicate];
[output addObjectsFromArray:filtered];
NSLog(@"%@",output);
【讨论】:
我使用了 NSMutableArray 下面还有一个错误:什么是arrayofDay,什么是OrignalArray 原始数组是输入数组。我将 arrayofDay 编辑为天 您正在输入数组的原始数组上添加过滤器,并且该过滤器正在检查“originalArray”中的每个元素是否包含天数组中的字符串 NSArray* 过滤错误:使用不兼容类型“void”的表达式初始化“NSArray *__strong”以上是关于如何获取 NSMutableArray 特定对象的主要内容,如果未能解决你的问题,请参考以下文章
如何获取 NSMutableArray 的 indexPath 值?
如何将特定 NSMutableArray 中的数据重新加载到 UITableView