2017-03-12 1 views
0

macOS Sierra, iMac, swift3changement MacOS et rapide/fichier mis creationDate

Je suis en train d'écrire un programme qui va changer la date de création d'un fichier. J'ai beaucoup de photos numérisées et de documents que je souhaite pouvoir stocker dans l'ordre alphabétique ou chronologique.

Je l'ai atteint en Objective-C, mais je suis en difficulté. La méthode d'API Apple> Foundation> FileManager> setAttributes devrait résoudre mon problème. méthode d'instance ----- setAttributes (: ofItemAtPath :) Déclaration --- f ** c setAttributes ( attributs: [FileAttributeKey: Tout], ofItemAtPath path: String) renvoie

je ne peux pas écrire cette ligne de code pour qu'elle compile.

var isChangedBoolean = try fm.setAttributes(myAttributesDictionary: [FileAttributeKey : creationDate],ofItemAtPath : myPath) 
############## Mon code
let fm = FileManager() 
    let myPath = "/Users/jon/a-1.jpg" //original date 30/1/2013 = d/m/y 

    //Convert date string to date object 
    let myDateString = "24/11/2014" // required date 
    let dateFmt = DateFormatter() 
    //dateFmt.timeZone = NSTimeZone.default 
    dateFmt.dateFormat = "dd/MM/yyyy" 
    let myDateObject = dateFmt.date(from : myDateString) 
    print("myDateString :\(myDateString) myDateObj :\(myDateObject!)") 

    //declare Mutable Dictionary with key : creationDate and my value 
    var myAttributesDictionary = [FileAttributeKey : Date]() 
    myAttributesDictionary[FileAttributeKey.creationDate] = myDateObject 

    print("----------------- mtAttributesDictionary") 
    print(myAttributesDictionary) 
    print(myAttributesDictionary[FileAttributeKey.creationDate]!) // works 
    print("### Will print myAttributesDictionary and a value for the key") // works 

    do 
    { 
     let fileAttributes = try fm.attributesOfItem(atPath : myPath) //works 
     //print(fileAttributes) 
     print("### Will print File Attributes") 
    } 
    catch let error as NSError 
    { print("ERROR READ Attributes: \(error)") } 


    do 
    { 
     print("### Will print my creationDateObject :: \(myAttributesDictionary[FileAttributeKey.creationDate]!)") 
// This line is my problem. I cannot write a line of code that will compile 
// var isChangedBoolean = try fm.setAttributes(myAttributesDictionary: [FileAttributeKey : creationDate],ofItemAtPath : myPath) 
    } 
    catch let error as NSError 
    { print("ERROR ERROR in setAttributes: \(error)") } 

    } 

sortie ----- myDateString: 24/11/2014 myDateObj: 23/11/2014 13 : 00: 00 +0000 myAttributesDictionary [__C.FileAttributeKey (_rawValue: NSFileCreationDate): 2014-11-23 13:00:00 +0000] 2014-11-23 13:00:00 +0000 ### Will print myAttributesDictionary et une valeur pour la clé ### Permet d'imprimer les attributs de fichier ### J'imprimerai creationDateObject :: 2014-11-23 13:00:00 +0000

Si la ligne dans le code de la question est décommentée, elle ne sera pas compilée. Je l'ai écrit au moins 50 fois et j'ai eu beaucoup de différents types d'erreurs de compilateur. Je ne comprends tout simplement pas ce que l'API requiert et je ne trouve pas d'exemple concret. Toutes les suggestions seraient grandement appréciées. Une solution serait encore plus appréciée. Merci.

Répondre

0

Ai-je compliqué la tâche?

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