如何在复杂的 NSMutableArray 上搜索内容

How to search something on a complex NSMutableArray

我有一个NSMutableArray。它有 8 个对象。每个对象有 5 个这样的键值对

{
    "Attendance": {}, 
    "Shift": {}, 
    "DayType": {}, 
    "DayTypes": null, 
    "TotalOvertimeString": "06:00"
}, 

而这个 "Attendance" 也是另一本词典。是这样的

"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"
    }, 

我想做的是在这个数组中找到这个确切的日期值是否可用 "Date": "2016-02-01T00:00:00"我如何可以搜索这个日期在这个数组中是否可用?请帮助我。

试试下面的代码行 [数组 valueForKey @"Attendance"]valueForKey@"Date"]isEqualToString:@"2016-02-01T00:00:00" ]

希望对您有所帮助。

您可以从中获取数据并通过以下方式进行比较。

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]

我想你可以试试这个

([yourArray@[0] valueForKey@"Date"] compare:dateYouEntered == NSOrderedSame)

那么您输入的日期存在于您的数组中

对于这种嵌套结构,您可以使用 valueForKeyPath 而不是 valueForKey。第一个对象是字典。

NSDictionary *dict = @{
    @"Attendance": @{...,@"Date": @"2016-02-01T00:00:00",....}, 
    @"Shift": @{}, 
    @"DayType": @{}, 
    @"TotalOvertimeString": @"06:00"
};

使用下面的代码行:

[[dict valueForKeyPath:@"Attendance.Date"] isEqualToString:@"your date"]

处理@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.`
   }

containsObject 方法returns如果数组中存在 anObject,则为 YES,否则为 NO。

希望对您有所帮助。快乐编码..!!