2012-01-02 60 views
1

我目前正在從遠程服務器進行文件更新。我可以下載該文件並將其保存到文檔目錄中。該文件有一個「Last-Modified」標籤,我正在使用它來檢查文件是否需要更新。但我的問題是,你如何保存帶有標籤的字符串以備後用?稍後我想將保存的字符串與另一個字符串與當前的「Last-Modified」標記進行比較。如果它們相同,則文件不必更新,但如果它們不相等,我將下載新文件。如何保存字符串以備後用?

對不起,英文錯誤,請更正我和任何幫助表示讚賞。一直在掙扎着這一段時間!

編輯:

NSDictionary *metaData = [test allHeaderFields]; 

//NSLog(@"%@", [metaData description]); 

lastModifiedString = [metaData objectForKey:@"Last-Modified"]; 

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults]; 
[standardUserDefaults setObject:lastModifiedString forKey:@"LastModified"]; 
[standardUserDefaults synchronize]; 

NSString *savedString = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastModified"]; 

if (![lastModifiedString isEqualToString:savedString]) 
{ 
    [self downloadNewFile]; 
} 

下載鏈接到文件:Archive.zip

回答

2

使用NSUserDefaults或核心數據持久化的值。

編輯:

它不工作,因爲您檢索之前保存新值。你需要移動

NSString *savedString = [[NSUserDefaults standardUserDefaults] stringForKey:@"LastModified"]; 

上述

[standardUserDefaults setObject:lastModifiedString forKey:@"LastModified"]; 

現在,您可以比較反對舊用戶的默認值的新文件的值。

+0

@Peter_DeWeese我曾嘗試使用NSUserDefaults,但它不適合我。如果您想查看,我已將代碼添加到該問題中。謝謝! – Jacob 2012-01-02 18:20:25

+0

下一次只需剪掉關鍵部分並將其包含在您的帖子中。我現在就爲你做,然後編輯我的答案。 – 2012-01-02 19:12:21

+0

是的,我會的,謝謝! – Jacob 2012-01-02 19:36:34

0

你說你想比較最後修改的日期,看看它們是否一樣?

我認爲最好的辦法是將日期(字符串)保存在Property List中。如果您正在製作iPhone應用程序,則可以使用下面的代碼創建包含字符串的屬性列表。這段代碼檢查一個文件是否已經存在,如果存在,就從中讀取文件,如果沒有,就創建一個文件並寫入文件。

// Get the path to the property list. 
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *pathToPlist = [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"yourfilename.plist"]; 
// Check whether there is a plist file that already exists. 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pathToPlist]; 

if (!fileExists) { 
    // There is no file so we set the file up and save it. 
    NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithCapacity:1]; 
    [newDict setObject:yourStringWithTheLastModifiedDate forKey:@"lastModified"]; 
    [newDict writeToFile:pathToPlist atomically:YES]; 
} 

} else { 
    // There is already a plist file. You could add code here to write to the file rather than read from it. 
    // Check the value of lastModified. 
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:pathToPlist]; 
    NSString *lastModifiedDate = [persistentNonResidentData objectForKey:@"lastModified"]; 

    // Add your own code to compare the strings. 
} 

或者我可能誤解了你的問題,這可能不是你所要找的所有lol。

+0

感謝您的迴應,但這不是我正在尋找的。 – Jacob 2012-01-02 18:22:23

相關問題