使用 Swift 归档、取消归档和清理缓存中的对象
Archiving, unarchiving and cleaning objects in cache with Swift
我在处理缓存中某些 类 的持久性时遇到问题。这是我试过的代码,无论是恢复归档数据还是删除它,它都不起作用:
func cacheDirectoryURL()->NSURL?{
let fileManager=NSFileManager()
let urls = fileManager.URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask)
if urls.count>0{
return urls[0]
}
return nil
}
func palineCacheURL()->NSURL?{
let url=cacheDirectoryURL()?.URLByAppendingPathExtension("Paline.archive")
return url
}
public func getMapHandlerWithDelegate(delegate:MovableAnnotationDelegate)->MapDelegate{
let archiveURL=palineCacheURL()!
mapHandler=NSKeyedUnarchiver.unarchiveObjectWithFile(archiveURL.absoluteString) as? MapDelegate
if mapHandler != nil{
mapHandler!.delegate=delegate;
} else {
mapHandler=MapDelegate.sharedMapDelegate()
mapHandler!.delegate=delegate;
}
return mapHandler!
}
func clearMapArchives(){
do{
try NSFileManager.defaultManager().removeItemAtPath(palineCacheURL()!.absoluteString);
} catch let error as NSError {
print(error.localizedDescription+" "+(error.localizedFailureReason ?? ""));
let alert=UIAlertView(title:NSLocalizedString("Error", comment:""), message:NSLocalizedString("Unable to clean the cache; please reinstall the app in the case of problems", comment:""), cancelButtonTitle:NSLocalizedString("Dismiss", comment:""), otherButtonTitle:nil, onDismiss:nil, onCancel:nil)
alert.show()
}
}
func archive(){
//-NSLog(@"archivePath=%@ paline=%@", archivePath, self.palineArray);
if mapHandler != nil {
NSKeyedArchiver.archiveRootObject(mapHandler!, toFile: palineCacheURL()!.absoluteString)
print("archivio a \(palineCacheURL()!.absoluteString)")
}
}
我什至尝试在保存后立即检索该对象,但结果为零:
NSKeyedArchiver.archiveRootObject(mapHandler!, toFile: palineCacheURL().absoluteString)
print("archivio a \(palineCacheURL().absoluteString)")
let restored=NSKeyedUnarchiver.unarchiveObjectWithFile(palineCacheURL().absoluteString) as? MapDelegate
print("restored \(restored)")
非常感谢 fluidsonic 的提示,除了将 absoluteString 替换为路径外,代码还不错。一旦更换了这部分,一切都会无缝运行。
我在处理缓存中某些 类 的持久性时遇到问题。这是我试过的代码,无论是恢复归档数据还是删除它,它都不起作用:
func cacheDirectoryURL()->NSURL?{
let fileManager=NSFileManager()
let urls = fileManager.URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask)
if urls.count>0{
return urls[0]
}
return nil
}
func palineCacheURL()->NSURL?{
let url=cacheDirectoryURL()?.URLByAppendingPathExtension("Paline.archive")
return url
}
public func getMapHandlerWithDelegate(delegate:MovableAnnotationDelegate)->MapDelegate{
let archiveURL=palineCacheURL()!
mapHandler=NSKeyedUnarchiver.unarchiveObjectWithFile(archiveURL.absoluteString) as? MapDelegate
if mapHandler != nil{
mapHandler!.delegate=delegate;
} else {
mapHandler=MapDelegate.sharedMapDelegate()
mapHandler!.delegate=delegate;
}
return mapHandler!
}
func clearMapArchives(){
do{
try NSFileManager.defaultManager().removeItemAtPath(palineCacheURL()!.absoluteString);
} catch let error as NSError {
print(error.localizedDescription+" "+(error.localizedFailureReason ?? ""));
let alert=UIAlertView(title:NSLocalizedString("Error", comment:""), message:NSLocalizedString("Unable to clean the cache; please reinstall the app in the case of problems", comment:""), cancelButtonTitle:NSLocalizedString("Dismiss", comment:""), otherButtonTitle:nil, onDismiss:nil, onCancel:nil)
alert.show()
}
}
func archive(){
//-NSLog(@"archivePath=%@ paline=%@", archivePath, self.palineArray);
if mapHandler != nil {
NSKeyedArchiver.archiveRootObject(mapHandler!, toFile: palineCacheURL()!.absoluteString)
print("archivio a \(palineCacheURL()!.absoluteString)")
}
}
我什至尝试在保存后立即检索该对象,但结果为零:
NSKeyedArchiver.archiveRootObject(mapHandler!, toFile: palineCacheURL().absoluteString)
print("archivio a \(palineCacheURL().absoluteString)")
let restored=NSKeyedUnarchiver.unarchiveObjectWithFile(palineCacheURL().absoluteString) as? MapDelegate
print("restored \(restored)")
非常感谢 fluidsonic 的提示,除了将 absoluteString 替换为路径外,代码还不错。一旦更换了这部分,一切都会无缝运行。