2017-03-12 7 views
1

macOS, swift3
von Apple hat eine API Foundation> Dateimanager> setAttributes (_: ofItemAtPath :)Verwenden schnell und macOS-Datei-Attribute

Die Erklärung ist func setAttributes (_ Attribute: [FileAttributeKey: Jedes] , ofItemAtPath Pfad: String) wirft

Es ist für das Festlegen des Erstellungsdatums usw. für eine Datei. Ich kann mit dem> ofItem-Pfad umgehen: String) wirft <, aber der erste Teil hat mich ratlos.

Die API sagt, dass es "wahr" zurückgeben kann, aber schnelle Rückgabe ungültig. Es gibt ein Attribut namens 'creationDate'. Was ist die Bedeutung des Unterstrichs '_'?
Ich denke, ‚Attribute‘ ist ein veränderliches Wörterbuch

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

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

ich viele Variationen versucht haben, und jetzt bin ich ratlos und ich weiß nicht, was erforderlich ist. Ich kann die setAttributes Zeile nicht zum kompilieren

Antwort

0

Ich verbrachte einige Zeit auf der Suche nach einer Antwort vor dem Posten der Frage. Als ich nach meiner Frage suchte, fand ich eine Antwort.

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) 
    } 
Verwandte Themen