Skip to content

【Swift】ファイルのサイズや更新日時の取得方法

Swiftでファイルのサイズや更新日時の取得方法を紹介します。

FileManagerattributesOfItemAtPathメソッドを使用することで簡単に取得することが出来ます。

let path = "ファイルのパス"
do {
    let attributes = try FileManager().attributesOfItem(atPath: path)
    print(attributes[.size]!)
    print(attributes[.modificationDate]!)
} catch {
    print("エラー")
}

ファイルサイズはバイトで取得されます、キロバイト表示にしたい場合などは1024で割ってあげる必要があります。

https://developer.apple.com/documentation/foundation/nsfilemanager/1410452-attributesofitematpath

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です