如何保存字符串以供以后使用?
我目前正在努力从远程服务器更新文件。我可以下载该文件并将其保存到文档目录中。该文件有一个“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 NSUserDefaults或核心数据来保存一个值。
编辑:
它不起作用,因为您在检索新值之前保存它。您需要移至
上方。
现在您将把新文件值与旧用户默认值进行比较。
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
above
Now you'll be comparing the new file value against the old user defaults value.
您是说您想比较上次修改日期以查看它们是否相同?
我认为最好的方法是将日期(作为字符串)保存在 属性列表。如果您正在制作 iPhone 应用程序,您可以使用以下代码创建带有字符串的属性列表。此代码检查文件是否已存在,如果存在,则从该文件中读取,如果不存在,则创建一个文件并写入该文件。
或者我可能误解了你的问题,这可能根本不是你想要的,哈哈。
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.
Or I may have misunderstood your question and that may not be what you are looking for at all lol.