2017-03-12 11 views
0

는 맥 OS 시에라, 아이맥, 나는 파일의 생성 날짜를 변경하는 프로그램을 작성하는 것을 시도하고있다맥 OS 및 신속한 변경/설정 파일에서 CreationDate는

을 swift3. 알파벳순 또는 연대순으로 저장할 수있는 스캔 한 사진과 문서가 많습니다.

나는 Objective-C에서 이것을 달성했지만 신속하게 고군분투하고 있습니다. Apple API> Foundation> FileManager> setAttributes 인스턴스 메소드가 내 문제를 해결해야합니다. 인스턴스 방법 ----- setAttributes (: ofItemAtPath : 선언 --- f를 ** C setAttributes ( 속성 : [FileAttributeKey : 모든, ofItemAtPath 경로 : 문자열) 내가 쓸 수 없습니다입니다

를 던졌습니다 이 코드 라인은 컴파일 할 수 있도록합니다.

var isChangedBoolean = try fm.setAttributes(myAttributesDictionary: [FileAttributeKey : creationDate],ofItemAtPath : myPath) 
############## 내 코드
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)") } 

    } 

출력 ----- myDateString : 24/11/2014 myDateObj : 2014년 11월 23일 (13) : 00 : 00 +0000 myAttributesDictionary [__C.FileAttributeKey (_rawValue : NSFileCreationDate) : 2014-11-23 13:00:00 +0000] 2014-11-23 13:00:00 +0000 ### 윌 myAttributesDictionary 및 키 값 인쇄 ### 파일 속성 인쇄 ### 내 creationDateObject를 인쇄합니다 .DateObject :: 2014-11-23 13:00:00 +0000

문제 코드의 줄이 주석 처리되지 않은 경우 컴파일되지 않습니다. 필자는 적어도 50 번 이상 썼으며 많은 종류의 컴파일러 오류가있었습니다. API가 필요로하는 것이 무엇인지 이해하지 못하고 실제로 예제를 찾을 수 없습니다. 제안 사항을 보내 주시면 감사하겠습니다. 해결책은 더 높이 평가 될 것입니다. 감사합니다.

답변

0

나는 이것을 복잡하게 만들었습니까?

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