如何保存字符串以供以后使用?

发布于 2024-12-23 16:46:31 字数 904 浏览 5 评论 0原文

我目前正在努力从远程服务器更新文件。我可以下载该文件并将其保存到文档目录中。该文件有一个“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

I am currently working on having a file update from a remote server. I am able to download the file and save it to the documents directory. The file has a "Last-Modified" tag and I am using it to check if the file needs to be updated. But my question is, how do you save the string with the tag for later use? Later I want to compare the saved string with the another string with the current "Last-Modified" tag. If they are equal the file doesn't have to be updated but if they're not equal I will download the new file.

Sorry for bad English, correct me and any help is appreciated. Have been struggling with this for a while!

EDIT:

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];
}

Download link to files: Archive.zip

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚你发端 2024-12-30 16:46:31

使用 NSUserDefaults或核心数据来保存一个值。

编辑:

它不起作用,因为您在检索新值之前保存它。您需要移至

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

上方。

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

现在您将把新文件值与旧用户默认值进行比较。

Use NSUserDefaults or Core Data to persist a value.

EDIT:

It is not working because you are saving the new value before retrieving it. You'll need to move

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

above

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

Now you'll be comparing the new file value against the old user defaults value.

提笔落墨 2024-12-30 16:46:31

您是说您想比较上次修改日期以查看它们是否相同?

我认为最好的方法是将日期(作为字符串)保存在 属性列表。如果您正在制作 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.
}

或者我可能误解了你的问题,这可能根本不是你想要的,哈哈。

You are saying you want to compare the Last Modified dates to see if they are the same?

I think the best way to do this would be to save the date (as a string) in a Property List. If you are making an iPhone app you can create a property list with a string with the code below. This code checks if a file already exists and if it does, it reads from it, and if it doesn't, it creates one and writes to it.

// 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.
}

Or I may have misunderstood your question and that may not be what you are looking for at all lol.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文