19

このコードを使用して、ファイルの最終変更日を取得しようとしています。

NSError *error = nil;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath: myFilePath error:&error];

        if (attributes != nil) {
            NSDate *date = (NSDate*)[attributes objectForKey: NSFileModificationDate];
            NSLog(@"Date modiifed: %@", [date description]);
        }
        else {
            NSLog(@"Not found");
        }

これは、メインバンドル内のファイルには適切に機能しますが、ファイルがアプリのドキュメントフォルダーのサブディレクトリにある場合は、次のように機能しませんmyFilePath

/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/The App ID Number/Documents/mySubdirectory/My Saved File

「見つかりません」を返し続けます。

ファインダーで表示できるので、ファイルがそこにあることがわかります。また、ファイル名のスペースを削除してみましたが、効果がありませんでした。

エラーログにはそのようなファイルやディレクトリは表示されないため、ファイルをドキュメントディレクトリにコピーしようとしたときに問題が発生したようです。

奇妙なことに、ドキュメントのサブディレクトリを繰り返し処理するとcontentsOfDirectoryAtPath、ファイルが存在することが示されます。

パスをハードコーディングしてプログラムで取得しようとしましたが、次のようになりました。

*myFolder = [documentsDirectory stringByAppendingPathComponent:@"myFolder"];
*myFilePath = [myFolder stringByAppendingPathComponent:theFileName];

誰かが私がどこで間違っているのか見ることができますか?

4

6 に答える 6

27

Swift 3ソリューション:

func fileModificationDate(url: URL) -> Date? {
    do {
        let attr = try FileManager.default.attributesOfItem(atPath: url.path)
        return attr[FileAttributeKey.modificationDate] as? Date
    } catch {
        return nil
    }
}
于 2016-11-06T08:25:08.960 に答える
23

これを試して。私は同じ問題を抱えていて、次のようなもので解決しました:

NSURL *fileUrl = [NSURL fileURLWithPath:myFilePath];
NSDate *fileDate;
[fileUrl getResourceValue:&fileDate forKey:NSURLContentModificationDateKey error:&error];
if (!error)
{
//here you should be able to read valid date from fileDate variable
}

それが役に立ったことを願っています;)

于 2012-11-22T16:28:19.783 に答える
6

これが@zvjerka24の答えのSwiftのような解決策です:

func lastModified(path: String) -> NSDate? {
    let fileUrl = NSURL(fileURLWithPath: path)
    var modified: AnyObject?
    do {
        try fileUrl.getResourceValue(&modified, forKey: NSURLContentModificationDateKey)
        return modified as? NSDate
    } catch let error as NSError {
        print("\(#function) Error: \(error)")
        return nil
    }
}
于 2016-08-30T20:12:42.647 に答える
3

エラーが発生した場合:

「スキームのないこのURLが渡されたため、CFURLCopyResourcePropertyForKeyが失敗しました」

NSURLに変換する前にNSStringファイルパスに「file:///」を追加することでこれを解決することができます。私の場合は機能しました。

于 2013-02-04T19:31:13.440 に答える
3

次のこともできます:

NSURL* file = ...
NSError* error;`
NSDate *creationDate = [[NSFileManager defaultManager] attributesOfItemAtPath:file.path error:&error].fileCreationDate;
于 2015-12-21T04:24:25.363 に答える
1

macOSシステムのどのファイルでも、以下のオプションのいずれかを使用して、変更日を簡単に取得できます。

方法1:

  • NSString * path =@"ファイルへのパス";
  • NSError * err = nil;
  • NSDictionary * dic2 = [[NSFileManager defaultManager] attributeOfItemAtPath:path error:&err];
  • NSLog(@ "ファイル変更日:%@"、dic2 [NSFileModificationDate]);

方法2:

  • MDItemRef itemRef = MDItemCreate(kCFAllocatorDefault、(__bridge CFStringRef)path);
  • NSArray * attributeNames =(__bridge NSArray *)MDItemCopyAttributeNames(itemRef);
  • NSDictionary * attributes =(__bridge NSDictionary *)MDItemCopyAttributes(itemRef、(__bridge CFArrayRef)attributeNames);

  • CFDateRef modifDate = MDItemCopyAttribute(itemRef、kMDItemContentModificationDate);

  • NSDate * modifyDate =(__bridge NSDate *)modifDate;
  • NSLog(@ "変更日%@"、変更日);

MDItemによって提供される他のさまざまな属性を出力することもできます。NSLog(@ "すべての属性%@"、attributes);

于 2019-02-25T07:52:49.660 に答える