【问题标题】:Use swift and macOS to set file attributes使用swift和macOS设置文件属性
【发布时间】:2017-08-02 07:32:40
【问题描述】:

macOS、swift3
苹果有一个 API Foundation > FileManager > setAttributes(_:ofItemAtPath:)

声明是 func setAttributes(_ attributes: [FileAttributeKey : Any], ofItemAtPath path: String) throws

用于设置文件的创建日期等。 我可以处理 >ofItem path:String) throws

API 说它可以返回“true”,但 swift 返回 void。有一个名为“creationDate”的属性。下划线'_'的意义是什么。
我认为“属性”是一个可变字典

 var myAttributesDictionary = [FileAttributeKey : Date]()
 myAttributesDictionary[FileAttributeKey.creationDate] = myDateObject

let fm = FileManger()
let xxx = fm.setAttributes(myAttributesDictionary:[FileAttributeKey : creationDate], ofItemAtPath myPath)

我尝试了很多变化,现在我很困惑,我不知道需要什么。我无法编译 setAttributes 行

【问题讨论】:

    标签: macos swift3 file-attributes


    【解决方案1】:

    在发布问题之前,我花了一些时间寻找答案。 当我去寻找我的问题时,我找到了答案。

    let mypath = "/path/to/file"
    let myDateObject = NSDate()       // NSDate()  is todays date
    
    let attributes = [FileAttributeKey.creationDate: myDateObject]
    
     do {
            try FileManager.default.setAttributes(attributes, ofItemAtPath: myPath)
      } 
      catch 
      {
            print(error)
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-22
      • 2016-01-06
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      相关资源
      最近更新 更多