我的文件夹包含音频和文本文件
我需要获取最后创建的文本文件和音频文件路径,而不在文件夹上循环或不使用可变数组和使用修改数据的顺序
任何想法
答案 0 :(得分:3)
有两种解决方案:
循环实现:
NSString *documentsDirectory = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] path];
NSArray *docFileList = [[NSFileManager defaultManager] subpathsAtPath:documentsDirectory];
NSEnumerator *docEnumerator = [docFileList objectEnumerator];
NSString *docFilePath;
NSDate *lastModifiedDate = [NSDate dateWithTimeIntervalSince1970:0];
NSString *lastModifiedFilePath = @"";
while ((docFilePath = [docEnumerator nextObject])) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:docFilePath];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil];
NSDate currentModifiedDate = [fileAttributes fileModificationDate];
if (lastModifiedDate < fileModificationDate) {
fileModificationDate = lastModifiedDate;
lastModifiedFilePath = fullPath;
}
}
return lastModifiedFilePath;
答案 1 :(得分:0)
下载文件时的另一个选项是,您需要管理自动生成的.plist文件以及最后一个值获取并证明最新下载文件名称的.p文件。
在这里显示一个教程。 以下代码用于下载音频文件并添加动态创建的.plist
NSString *strAudioURL=@"Your sound ";
// check first locally exists or not
NSString *strPathToAudioCache=[NSString stringWithFormat:@"%@/%@",
[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
AudioFolder];
NSDictionary *dOfAudios=[NSDictionary dictionaryWithContentsOfFile:strPathToAudioCache];
if([dOfAudios valueForKey:strAudioURL]) {
[APP_DEL initAndPlayMovie:[NSURL fileURLWithPath:[dOfAudios valueForKey:strAudioURL]] andViewController:self];
} else {
NSURL *audioURL = [NSURL URLWithString:strAudioURL];
NSString *strPathToDownload=[NSString stringWithFormat:@"%@/%@",[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],
[strAudioURL lastPathComponent]];
if(!self.rqstForAudio || [self.rqstForAudio isFinished]) {
self.rqstForAudio=[ASIHTTPRequest requestWithURL:audioURL];
[self.rqstForAudio setDelegate:self];
[self.rqstForAudio setAllowResumeForFileDownloads:YES];
[self.rqstForAudio setCachePolicy:ASIUseDefaultCachePolicy];
[self.rqstForAudio setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[self.rqstForAudio setDidFailSelector:@selector(failedToLoad:)];
[self.rqstForAudio setDidFinishSelector:@selector(finishedLoading:)];
[self.rqstForAudio setDownloadCache:[ASIDownloadCache sharedCache]];
[self.rqstForAudio setDownloadDestinationPath:strPathToDownload];
[self.rqstForAudio startAsynchronous];
}
}