I'm trying to do something in swift, and having trouble getting it to work.
I'm attempting to take a flat list of files, and create a date based directory tree.
2014
01
2014-01-06
fred.jpg
2014-01-15
harry.jpg
09
2014-09-07
george.jpg
2015
02
2015-02-03
ron.jpg
Step 1 is to create hierarchical dictionaries to build a tree of the nested folders to create.
I am able to get an enumeratable list of files, ie the following code appears to be working as expected...
typealias yearFolders = [String: monthFolders] // use year as the key eg. "2014"
typealias monthFolders = [String: dayFiles] // use month as the key eg. "01"
typealias dayFiles = [String: [String]] // use date as the key eg. "2014-01-06"
...// create my list of files
var allFiles: yearFolders = [:]
while let fileDetails = enumerator?.nextObject() as? NSURL {
if !(fileDetails.isDirectory())
{
if let createDate = fileDetails.creationDate() {
addFileAt(fileDetails.absoluteString, forDate: createDate, inStructure: &allFiles)
}
}
}
but am having trouble getting a non-ugly version of addFileAt() to work
when I do this
func addFileAt(path: String, forDate date: NSDate, inout inStructure structure: yearFolders)
{
let yearFolderName = yearFormatter.stringFromDate(date)
if (structure[yearFolderName] == nil) {
structure[yearFolderName] = [:]
}
var yearFolder = structure[yearFolderName]!
let monthFolderName = monthFormatter.stringFromDate(date)
if (yearFolder[monthFolderName] == nil) {
yearFolder[monthFolderName] = [:]
}
line 06. successfully adds an empty dictionary (of type monthFolders) to structure
line 08. creates a reference to this empty monthFolders dictionary
line 12. then adds an empty dayFiles dictionary to the yearFolder variable created in line 08.
However yearFolder is NOT the object that was added to structure. it seems to be a copy.
Is there a 'nice' way for me to create yearFolder as a reference to structure[yearFolderName] rather than copy?
when I change lines 11 & 12 as below, everything works, but it just feels a bit hard to read (and less robust)
if (structure[yearFolderName]![monthFolderName] == nil) {
structure[yearFolderName]![monthFolderName] = [:]
}
any/all guidance appreciated,
Mike