2013-03-16 51 views
-2

我想比較兩個日期,當前日期和時間在我的plist。如果我的plist中的當前日期和日期相等,那麼我想顯示一些內容。可能嗎。請幫幫我。我非常迫切需要回答。我想要檢索的iOS設備的plist數據編程

在plist中的層次結構陣列 - >詞典 - >(對象被認爲是用於一個陣列和鍵應該是相同的所有對象的接觸和它是一個字符串)。我想比較鍵和當前日期。

+2

你嘗試過什麼到目前爲止urrent日期?你知道如何比較兩個日期嗎?你知道如何加載plist文件嗎?顯示目前爲止的代碼(與此需求有關)。 – rmaddy 2013-03-16 05:23:40

回答

2

您可以使用下面的代碼。

NSString *path = [[NSBundle mainBundle] pathForResource:@"EventAddress" ofType:@"plist"]; 
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; 

NSArray* allmyKeys = [myDictionary allValues]; 
NSLog(@"%@", allmyKeys); 
NSLog(@"%@", [[allmyKeys objectAtIndex:0] objectAtIndex:0]); 

從貸款機構 獲取日期您的plist日期應該包含這樣的:

<date>2011-12-13T00:00:00Z</date> 

,並從NSDictionary中使用獲取日期:

NSDate *eventDate = [myDictionary objectForKey:@"date"]; 

,並比較兩個日期的使用:

switch ([[NSDate date] compare:eventDate]){ 
case NSOrderedAscending: 
     NSLog(@"NSOrderedAscending"); 
break; 
case NSOrderedSame: 
     NSLog(@"NSOrderedSame"); 
break; 
case NSOrderedDescending: 
    NSLog(@"NSOrderedDescending"); 
break; 
} 

希望這會爲你工作。

所有最好的!

+0

這隻顯示如何加載plist。問題是關於獲取日期並將其與當前日期進行比較。 – rmaddy 2013-03-16 05:22:46

+0

請參閱最新的答案 – Yashesh 2013-03-16 05:33:40

0

爲了滿足這種「在plist中的層次結構是陣列 - >詞典 - >(對象是被視爲一個陣列和鍵應該是相同的所有對象的接觸和它是一個字符串)。我想比較當前日期的鑰匙」,你需要按照以下步驟

  1. 抓取所有陣列從文件的.plist
  2. 獲取,當前的數組對象
  3. 內的所有字典獲取,當前的字典中的所有鍵對象
  4. 比較你的KEY和C使用NSComparisonResult

    NSDate *date1 = < YOUR CURRENT DATE HERE > ; 
    NSString *plistFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"SyncAuditMainScreen.plist"]; 
    
    NSArray *array = [NSArray arrayWithContentsOfFile:plistFilePath]; 
    for(int i=0;i<[array count];i++) 
    { 
        NSMutableDictionary *details=[array objectAtIndex:i]; 
        NSArray *allmyKeys = [details allKeys]; 
        for(NSString *key in allmyKeys) 
        { 
         NSLog(@"KEY: %@",key); 
         NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
         [formatter setDateFormat:< YOUR FORMAT OF DATE >]; 
         NSDate *date2 = [formatter dateFromString:key]; 
         NSComparisonResult result = [date1 compare:date2]; 
         if(result==NSOrderedSame) 
          NSLog(@"Key and current date are same"); 
        } 
    }