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

4

2 に答える 2

2

NSUserDefaultsまたは Core Data を使用して値を永続化します。

編集:

新しい値を取得する前に保存しているため、機能していません。移動する必要があります

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

その上

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

ここで、新しいファイルの値を古いユーザーのデフォルト値と比較します。

于 2012-01-02T17:34:33.447 に答える
0

Last Modified の日付を比較して、それらが同じかどうかを確認したいと言っていますか?

これを行う最善の方法は、日付を(文字列として) 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.
}

または、私はあなたの質問を誤解している可能性があり、それはあなたが探しているものではないかもしれません笑。

于 2012-01-02T17:44:22.690 に答える