我有eachRow.split(',')
。它有8个对象。每个对象都有5个键值对,如下所示
NSMutableArray
这个"出席率"另一本字典。就像这样
{
"Attendance": {},
"Shift": {},
"DayType": {},
"DayTypes": null,
"TotalOvertimeString": "06:00"
},
我想要做的是在此数组中找到这个确切的日期值是否可用"Attendance": {
"AttendanceCode": 5058,
"Date": "2016-02-01T00:00:00",
"EmployeeCode": 2941,
"ShiftCode": 1,
"DayTypeCode": 1,
"Expected": true,
"Midnight": false,
"Present": true,
"InTime": "2016-02-01T08:00:00",
"InTimeString": "08:00",
"OutTime": "2016-02-01T20:00:00",
"OutTimeString": "20:00",
"OutDate": "2016-02-01T00:00:00",
"InTime2": "1901-01-01T00:00:00",
"InTime2String": "00:00",
"InDate2": "1901-01-01T00:00:00",
"OutTime2": "1901-01-01T00:00:00",
"OutTime2String": "00:00",
"OutDate2": "1901-01-01T00:00:00",
"ApprovedOutTimeUsed": 0,
"ApprovedOutTime": "1901-01-01T00:00:00",
"Late": 0,
"LateString": "00:00",
"InLate": 0,
"InLateString": "00:00",
"MidLate": 0,
"MidLateString": "",
"OutLate": 0,
"OutLateString": "00:00",
"InOvertime": 0,
"InOvertimeString": "",
"OvertimeRate1": 6,
"OvertimeRate1String": "06:00",
"OvertimeRate1Approved": 0,
"OvertimeRate1ApprovedString": "00:00",
"OvertimeRate2": 0,
"OvertimeRate2String": "00:00",
"OvertimeRate2Approved": 0,
"OvertimeRate2ApprovedString": "00:00",
"OvertimeRate3": 0,
"OvertimeRate3String": "00:00",
"OvertimeRate3Approved": 0,
"OvertimeRate3ApprovedString": "00:00",
"OvertimeRate4": 0,
"OvertimeRate4String": "00:00",
"OvertimeRate4Approved": 0,
"OvertimeRate4ApprovedString": "00:00",
"OvertimeRate5": 0,
"OvertimeRate5String": "00:00",
"OvertimeRate5Approved": 0,
"OvertimeRate5ApprovedString": "00:00",
"OvertimeRate6": 0,
"OvertimeRate6String": "00:00",
"OvertimeRate6Approved": 0,
"OvertimeRate6ApprovedString": "00:00",
"OvertimeApprovalDone": false,
"Lieu": false,
"LieuFirstTaken": false,
"LieuSecondTaken": false,
"LieuPayment": 0,
"DayOff": false,
"DayOffTaken": false,
"LeaveTypeCode": 0,
"LeaveAmount": 0,
"LeaveTypeCode2": 0,
"LeaveAmount2": 0,
"ShortLeaveAmount": 0,
"Premium": 0,
"PremiumString": "00:00",
"PremiumApproved": 0,
"PremiumApprovedString": "00:00",
"Missing": 0,
"MissingString": "00:00",
"ClockNumber": 0,
"SectionCode": 0,
"WorkHours": 0,
"PeriodWorked": 0,
"WorkHrs": 0,
"Remarks": "uuuuuuuuuuuuuuus",
"Deleted": false,
"Status": true,
"CreatedBy": 0,
"CreatedDate": "1901-01-01T00:00:00",
"UpdatedBy": 9,
"UpdatedDate": "2016-01-13T22:48:03.737",
"DeletedBy": 0,
"DeletedDate": "1901-01-01T00:00:00"
},
如何在此数组中搜索此日期是否可用?请帮帮我。
答案 0 :(得分:1)
对于这种嵌套结构,您可以使用valueForKeyPath
代替valueForKey
。第一个对象是字典。
NSDictionary *dict = @{
@"Attendance": @{...,@"Date": @"2016-02-01T00:00:00",....},
@"Shift": @{},
@"DayType": @{},
@"TotalOvertimeString": @"06:00"
};
使用以下代码行:
[[dict valueForKeyPath:@"Attendance.Date"] isEqualToString:@"your date"]
答案 1 :(得分:0)
尝试以下代码行 [arrray valueForKey @“Attendance”] valueForKey @“Date”] isEqualToString:@“2016-02-01T00:00:00”]
希望这会对你有所帮助。
答案 2 :(得分:0)
您可以从中获取数据并按以下方式进行比较。
NSMutableArray *array = @[];//Array of dictionaries
NSDate *dateToCompare = [NSDate date];//Any date you want to compare
for (NSDictionary *dictionary in array) {
NSDate *dateAttendance = dictionary[@"Attendance"][@"Date"];
//Compare dates
if ([dateToCompare isEqualToDate:dateAttendance]) {
NSLog(@"Dates equal");
}
}
如果两个日期都在NSString中,您可以使用
[dateToCompareString isEqualToString:dateAttendanceString]
答案 3 :(得分:0)
我想你可以试试这个
([yourArray@[0] valueForKey@"Date"] compare:dateYouEntered == NSOrderedSame)
然后您输入的日期存在于yourArray
中答案 4 :(得分:0)
致力于@Muhammad Nabeel Arif回答。你只需要在你的NSMutableArray中迭代你的所有对象来获取数组中的所有日期。最后,您可以使用NSMutableArray的containsObject方法检查您的日期。
NSDate *dateToCompare = [NSDate date];//Any date you want to compare
NSMutableArray *allDates =[NSMutableArray alloc]init];
for (int i = 0; i<array.count; i++)
{
NSMutableDictionary *dictionary = @{};//YOur data
NSDate *dateAttendance = dictionary[@"Attendance"][@"Date"];//Compare dates
[allDates addObject:dateAttendance];
}
if([allDates containsObject:dateToCompare])
{
// Do whatever if it exists
}else
{
`// place your code here if its doesnt exist.`
}
如果数组中存在anObject,containsObject方法返回YES,否则返回NO。
希望它有所帮助。快乐的编码.. !!