2016-03-15 73 views
0

我有一個NSMutableArray。它有8個對象。每個對象具有這樣如何搜索複雜的NSMutableArray上的東西

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

而這個「出席」的另一字典5鍵值對。正是這樣的

"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"如何搜索這個日期是否可用在這陣?請幫幫我。

+1

由於您知道數組元素的結構,因此可以使用多種方法遍歷數組中的所有元素,例如'enumerateObjectsUsingBlock'。如果您的目標是找到一個方便的方法來判斷整個數組中是否有日期,我建議您將該數組轉換爲JSON字符串,並且可以將它看作一個長字符串,並且很容易找到結果。 – childrenOurFuture

回答

1

你可以對於這種嵌套結構,使用valueForKeyPath而不是valueForKey。這第一個對象是一個字典。

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

使用下面的一行代碼:

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

,因此我需要使用for循環? – user1960169

+0

如果您已經知道第一個對象中的密鑰名稱,即考勤,則不需要任何循環。如果您不知道密鑰名稱,則只需要爲包含8個字典對象的外部字典使用循環。 – PGDev

+0

但是如何爲每8個物體旅行?怎麼我有{ 「考勤」:{}, 「轉變」:{}, 「DayType」:{}, 「DayTypes」:空, 「TotalOvertimeString」: 「06:00」 },這種類型8個對象。 – user1960169

0

嘗試下面的代碼 [arrray valueForKey @ 「考勤」] valueForKey @ 「日期」] isEqualToString行:@ 「2016-02-01T00:00:00」]

希望這會幫助你。

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] 
+0

那麼我仍然需要使用foor循環?因爲我的陣列中有8個對象 – user1960169

+0

在這種情況下,您可以使用循環 –

0

製作@Muhammad納比爾·阿里夫答案。 ü只需要遍歷你的所有對象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方法在數組中存在anObject時返回YES,否則返回NO。

希望它有幫助。快樂編碼..!