Created
August 25, 2016 13:18
-
-
Save odrobnik/733681965fbc8f05cf48ed8aada29efe to your computer and use it in GitHub Desktop.
This file contains hidden or 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
extension Dictionary where Key: String, Value: NSFileWrapper | |
{ | |
mutating func renameBundleResource(oldName: String, newName: String) | |
{ | |
// remove extension | |
let prefix = (oldName as NSString).stringByDeletingPathExtension | |
// find all files belonging to this resource | |
let fileNames = keys.sort().filter { (key) -> Bool in | |
return key.hasPrefix(prefix) | |
} | |
// rename page image and user files | |
for fileName in fileNames | |
{ | |
// remove prefix | |
let suffix = fileName.stringByReplacingOccurrencesOfString(prefix, withString: "") | |
let newPrefix = (newName as NSString).stringByDeletingPathExtension | |
let newName = newPrefix + suffix | |
var fileWrapper = dict[fileName]! | |
// if the writing file name has changed we need a fresh file wrapper | |
if newName.hasPrefix("Proof") && fileWrapper.filename != newName | |
{ | |
fileWrapper = NSFileWrapper(regularFileWithContents: fileWrapper.regularFileContents!) | |
fileWrapper.filename = newName | |
} | |
self[fileName] = nil | |
self[newName] = fileWrapper | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment