如何在iPhone上设置attributesOfItemAtPath(例如修改日期/时间)
我知道我可以使用 attributesOfItemAtPath
来获取文件的修改时间/日期等等......但是有没有办法设置文件的修改日期/时间?
我看过 如何设置 a 的修改时间以编程方式文件?但它似乎不适用。
我问的原因是我使用 http://allseeing-i.com/ASIHTTPRequest/ (如下面)来获取文件...但是时间戳与服务器上的上次修改时间不同。我想使用 Last-Modified 标头来设置下载文件的日期/时间,以便我可以保持系统上的下载时间与服务器上的时间相同。
我的代码如下:
ASIHTTPRequest *requestFeed = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:kGZipSQLURL]];
[requestFeed setNumberOfTimesToRetryOnTimeout:2];
[requestFeed setDownloadDestinationPath:librarySQLGZipPath];
[requestFeed setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[requestFeed setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[requestFeed setSecondsToCache:60*60*24*30];
[requestFeed setDownloadCache:[ASIDownloadCache sharedCache]];
[requestFeed startSynchronous];
NSString *lastModified = [[requestFeed responseHeaders] objectForKey:@"Last-Modified"];
NSLog(@"Last Modified: %@",lastModified);
因此,字符串 lastModified
保存时间/日期戳。有没有办法确保将 librarySQLGZipPath
中的文件现在设置为 lastModified
中的日期/时间?使用当前方法,文件 librarySQLGZipPath
保存其下载时间,这对我来说是无效的。
谢谢!
Jann
稍后编辑:
因为我想很好地在此页面上放置如何执行此代码,并且仍然想感谢 Steven Kramer 的回答,我现在要编辑问题与我使用代码得到的答案:
而 NSDateFromRFC1123
是在这里找到的例程:http://blog.mro.name/2009/08/nsdateformatter-http-header/ (进行一些调整)将 Last-Modified 标头字符串更改为 NSDate 对象:
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[self NSDateFromRFC1123:lastModified] ,NSFileModificationDate, nil];
NSError *errorFile;
if ([NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:librarySQLGZipPath error: &errorFile])
{
NSLog(@"Set timestamp of file");
}
else {
NSLog(@"COULD NOT set timestamp of file");
}
<非常感谢史蒂文!
I know I can use attributesOfItemAtPath
to get modification time/date of file among other things... but is there a way to SET the modification date/time of a file?
I have looked at How to set the modification time of a file programmatically? but it does not seem to apply.
The reason I ask is I use http://allseeing-i.com/ASIHTTPRequest/ (as below) to fetch a file... however the timestamp is not kept the same as the Last-Modified time on the server. I want to use the Last-Modified header to set the date/time of the downloaded file so i can keep the downloaded time on my system the same as it is on the server.
My code follows:
ASIHTTPRequest *requestFeed = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:kGZipSQLURL]];
[requestFeed setNumberOfTimesToRetryOnTimeout:2];
[requestFeed setDownloadDestinationPath:librarySQLGZipPath];
[requestFeed setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[requestFeed setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[requestFeed setSecondsToCache:60*60*24*30];
[requestFeed setDownloadCache:[ASIDownloadCache sharedCache]];
[requestFeed startSynchronous];
NSString *lastModified = [[requestFeed responseHeaders] objectForKey:@"Last-Modified"];
NSLog(@"Last Modified: %@",lastModified);
So, the string lastModified
holds the time/date stamp. Is there a way to ensure that the file at librarySQLGZipPath
will now be set to the date/time in lastModified
? Using the current method, the file librarySQLGZipPath
holds the time it was downloaded, which to me is invalid.
Thanks!
Jann
EDIT LATER:
Because I wanted to nicely put on this page how to do the code for this and still wanted to give Steven Kramer credit for answering I am now gonna edit the question with the answer I got using code:
whereas NSDateFromRFC1123
is a routine found here: http://blog.mro.name/2009/08/nsdateformatter-http-header/ (with some adjustments) to change the Last-Modified header string into a NSDate object:
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:[self NSDateFromRFC1123:lastModified] ,NSFileModificationDate, nil];
NSError *errorFile;
if ([NSFileManager defaultManager] setAttributes:attrs ofItemAtPath:librarySQLGZipPath error: &errorFile])
{
NSLog(@"Set timestamp of file");
}
else {
NSLog(@"COULD NOT set timestamp of file");
}
Thanks so much Steven!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
-[NSFileManager setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error
Use
-[NSFileManager setAttributes:(NSDictionary *)attributes ofItemAtPath:(NSString *)path error:(NSError **)error