ファイルの情報を取得するにはFileManagerを使います。
まずは取得するためのコードから。
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