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:
Put user data indocuments/
. User data generally includes any files you might want to expose to the user—anything you might want the user to create,import,delete or edit. For a drawing app,user data includes any graphic files the user might create. For a text editor,it includes the text files. VIDeo and audio apps may even include files that the user has downloaded to watch or Listen to later.
Put app-created support files in thelibrary/Application support/
directory. In general,this directory includes files that the app uses to run but that should remain hIDden from the user. This directory can also include data files,configuration files,templates and modifIEd versions of resources loaded from the app bundle.
Remember that files indocuments/
andApplication Support/
are backed up by default. You can exclude files from the backup by calling-[NSURL setResourceValue:forKey:error:]
using theNSURlisExcludedFromBackupKey
key. Any file that can be re-created or downloaded must be excluded from the backup. This is particularly important for large media files. If your application downloads vIDeo or audio files,make sure they are not included in the backup.
Put temporary data in thetmp/
directory. Temporary data comprises any data that you do not need to persist for an extended period of time. Remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device. The system will periodically purge these files when your app is not running; therefore,you cannot rely on these files persisting after your app terminates.
Put data cache files in thelibrary/Caches/
directory. Cache data can be used for any data that needs to persist longer than temporary data,but not as long as a support file. Generally speaking,the application does not require cache data to operate properly,but it can use cache data to improve performance. Examples of cache data include (but are not limited to) database cache files and transIEnt,downloadable content. Note that the system may delete theCaches/
directory to free up disk space,so your app must be able to re-create or download these files as needed.
/Users/Ys/library/Developer/CoreSimulator/Devices/$(code)/data/Containers/Data/Application/$(code)/documents
翻译好麻烦,不翻译了
@H_301_77@ // 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 Foundationclass 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
[]
总结以上是内存溢出为你收集整理的swift 文件缓存全部内容,希望文章能够帮你解决swift 文件缓存所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)