swift 文件缓存
2016-06-23 16:01
260 查看
ios中也有沙盒的概念,就是本程序只能操作本程序中的沙盒部分的东西,如下图:
![](https://img-blog.csdn.net/20160623155313188?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
在哪里放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 in
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 the
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 in
can exclude files from the backup by calling
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 the
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 the
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 the
我在虚拟机上打印出来就是这个路径:
/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
做了一点简单的实现:
简单的调用测试如下:
log:
true
1466668253.0
true
[]
在哪里放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 in
Documents/. 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 the
Library/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 in
Documents/and
Application Support/are backed up by default. You
can exclude files from the backup by calling
-[NSURL setResourceValue:forKey:error:]using the
NSURLIsExcludedFromBackupKeykey.
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 the
tmp/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 the
Library/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 the
Caches/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
翻译好麻烦,不翻译了
// 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
[]
相关文章推荐
- Apple Swift学习教程
- 介绍 Fedora 上的 Swift
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift自定义iOS中的TabBarController并为其添加动画
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 简单分析Swift语言的一些基本特征
- 使用 Swift 语言编写 Android 应用入门
- Swift与C语言指针结合使用实例
- Swift心得笔记之控制流
- 用Swift构建一个简单的iOS邮件应用的方法
- 苹果公司推出的新编程语言Swift简介和入门教程
- Swift实现iOS应用中短信验证码倒计时功能的实例分享
- 使用Swift代码实现iOS手势解锁、指纹解锁实例详解
- 探讨Swift数组和字典
- Swift中的指针操作和使用详细介绍
- Swift代码实现冒泡排序算法的简单实例
- Swift中使用正则表达式的一些方法
- Swift心得笔记之运算符