Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created August 25, 2016 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odrobnik/733681965fbc8f05cf48ed8aada29efe to your computer and use it in GitHub Desktop.
Save odrobnik/733681965fbc8f05cf48ed8aada29efe to your computer and use it in GitHub Desktop.
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