swift 文件缓存
ios中也有沙盒的概念,就是本程序只能操作本程序中的沙盒部分的东西,如下图:
在哪里放APP文件呢,apple说是放在Documents下 Where You Should Put Your App’s Files To prevent the syncing and backup processes on iOS devices from taking a long time,be selective about where you place files. Apps that store large files can slow down the process of backing up to iTunes or iCloud. These apps can also consume a large amount of a user's available storage,which may encourage the user to delete the app or disable backup of that app's data to iCloud. With this in mind,you should store app data according to the following guidelines:
我在虚拟机上打印出来就是这个路径:
/Users/Ys/Library/Developer/CoreSimulator/Devices/$(code)/data/Containers/Data/Application/$(code)/Documents 翻译好麻烦,不翻译了
// refer to: // 1、https://developer.apple.com/library/prerelease/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW1 // 2、https://developer.apple.com/library/prerelease/content/documentation/Cocoa/Conceptual/Strings/Articles/readingFiles.html#//apple_ref/doc/uid/TP40003459-SW1
做了一点简单的实现:
protocol AbstractFile{ func read(file :String) -> String func write(file :String,contents :String) -> Bool func del(file :String) -> Bool func list() -> [String] } // Created by Yeshen on 15/7/9. // Copyright (c) 2015年 Landow. All rights reserved. // import Foundation class File :AbstractFile{ let GoblePath: String = "Documents/" var Folder: String? init(folde: String){ if let dirs : [String] = NSSearchPathForDirectoriesInDomains( NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask,true) as [String] { if(dirs.count > 0){ Folder = (dirs[0] as NSString).stringByAppendingPathComponent(folde) do { try NSFileManager.defaultManager().createDirectoryAtPath(GoblePath,withIntermediateDirectories: true,attributes: nil) } catch _ { } } } } func read(file :String) -> String { if let path = Folder?.appendingPathComponent(file){ if let contents = try? String(contentsOfFile: path,encoding: NSUTF8StringEncoding){ return contents } } return Strings.empty } func write(file :String,contents :String) -> Bool{ if let path = Folder?.appendingPathComponent(file){ var error: NSError? do { try contents.writeToFile(path,atomically: false,encoding: NSUTF8StringEncoding) } catch let error1 as NSError { error = error1 } return error == nil } return false } func del(file :String) -> Bool{ if let path = Folder?.appendingPathComponent(file){ var error: NSError? do { try NSFileManager.defaultManager().removeItemAtPath(path) } catch let error1 as NSError { error = error1 } return error == nil } return false } func list() -> [String]{ var files:[String] = Array<String>() if let path = Folder{ if let list = try? NSFileManager.defaultManager().contentsOfDirectoryAtPath(path){ let array = NSArray(array: list) for item in array{ files.append(item.description) } } } return files } } func appendingPathComponent(path:String) -> String{ return (self as NSString).stringByAppendingPathComponent(path) } 简单的调用测试如下:
let file = File(folde: Consts.file.con) print(file.write(IO.N.CurTime,contents: Time.getNow().description)) print(file.read(IO.N.CurTime)) print(file.del(IO.N.CurTime)) print(file.list()) log: true 1466668253.0 true [] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |