4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift5でファイルの更新日時を取得する方法

4
Posted at

ファイルの情報を取得するにはFileManagerを使います。
まずは取得するためのコードから。

main.swift
import Foundation
let manager = FileManager()
var attributes = try manager.attributesOfItem(atPath: "パス")
print(attributes[.modificationDate]!)

パスに任意のファイルのパスを入力することでそのパスの指すファイルの更新日時を取得することができます。
続いてこのコードの説明です。

let manager = FileManager()

まずFileManagerのインスタンスを作り、

var attributes = try manager.attributesOfItem(atPath: "パス")

attributesという変数にそのファイルの属性(attributes)を入れます。これは辞書型になっています。
この辞書のキーはhttps://developer.apple.com/documentation/foundation/fileattributekeyのAccessing File Attributesのところに書いてあります。
今回は更新日時なので.modificationDateを使っています。

最後に

最後まで読んでくださり、ありがとうございます。
もしこの記事の中に改善すべきところ、間違っているところがありましたら、コメントなどで教えてください。

参考

https://nshipster.com/filemanager/
https://developer.apple.com/documentation/foundation/fileattributekey
https://developer.apple.com/documentation/foundation/filemanager

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?