Created
August 15, 2016 22:08
-
-
Save jakekara/302ead72be5c1c73d96d532ab59f29ad to your computer and use it in GitHub Desktop.
Class for getting and seeing file attributes in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MetaEditor.swift | |
// Developed for meditor - http://github.com/jakekara/meditor/ | |
// Get and set file attributes | |
// | |
// Created by Jake Kara on 8/12/16. | |
// | |
import Foundation | |
let fileManager = NSFileManager.defaultManager() | |
public class MetaEditor : NSObject { | |
var fileUrl = "" | |
// Initialize with a file url | |
init(fileUrl : String) { | |
self.fileUrl = fileUrl | |
} | |
// Return the attributes of the file | |
func getMeta() -> NSDictionary? { | |
do { | |
let attr : NSDictionary? = try NSFileManager.defaultManager().attributesOfItemAtPath(self.fileUrl); | |
Swift.print (attr); | |
return attr; | |
} | |
catch let error as NSError { | |
Swift.print("Ooops! Something went wrong: \(error)") | |
return nil; | |
} | |
} | |
// Set the creation date (should have a more descriptive method name, | |
// but I wasn't planning on adding a modification date method initially. | |
func setDate(newDate:NSDate) { | |
do { | |
Swift.print ("Setting date"); | |
try NSFileManager.defaultManager().setAttributes([ | |
NSFileCreationDate: newDate],ofItemAtPath:self.fileUrl) | |
} catch { | |
Swift.print ("ERROR"); | |
} | |
} | |
// Set the modification date of | |
func setModificationDate(newDate:NSDate) { | |
do { | |
Swift.print ("Setting date"); | |
try NSFileManager.defaultManager().setAttributes([ | |
NSFileModificationDate: newDate],ofItemAtPath:self.fileUrl) | |
} catch { | |
Swift.print ("ERROR"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment