//
// DocumentActionViewController.swift
// UIControlDemo
//
// Created by on 14/12/10.
// Copyright (c) 2014年 马大哈. All rights reserved.
//
import UIKit
class DocumentActionViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
let leftBarButton:UIBarButtonItem = UIBarButtonItem(title: "移除data.plist", style: UIBarButtonItemStyle.Plain, target: self, action: "removePlistFile")
self.navigationItem.rightBarButtonItem = leftBarButton;
// println("\n程序document路径 : \(applicationDocumentPath())")
// 在document中是否存在 ImageOfGoods/data.plist 文件
if applicationFileExistAtPath(false, fileName: "data.plist", directory: "ImageOfGoods"){
var data = applicationReadDataToFileAtPath(dataTypeArray: true,fileName: "data.plist", directory: "ImageOfGoods") as NSArray
println("\n获取file数据 : \(data)")
}else{
var writeData = ["1","2","3","4","5"]
// 创建 ImageOfGoods 文件夹
applicationCreatFileAtPath(fileTypeDirectory: true, fileName: "ImageOfGoods", directory: "")
// 创建 data.plist 文件
applicationCreatFileAtPath(fileTypeDirectory: false, fileName: "data.plist", directory: "ImageOfGoods")
// 插入 数组
applicationWriteDataToFileAtPath(dataTypeArray: true , content: writeData, fileName: "data.plist", directory: "ImageOfGoods")
}
}
func removePlistFile() {
applicationRemoveFileAtPath("data.plist", directory: "ImageOfGoods")
}
// 获取程序的document
func applicationDocumentPath() ->String{
let application = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentPathString = application[0] as String
return documentPathString
}
// 拼接文件路径
func applicationFilePath(fileName: String ,directory: String) ->String {
var docuPath = applicationDocumentPath()
if directory.isEmpty {
return docuPath.stringByAppendingPathComponent(fileName)
}else{
return docuPath.stringByAppendingPathComponent("\(directory)/\(fileName)")
}
}
// 指定路径下是否存在 特定 “文件” 或 “文件夹”
func applicationFileExistAtPath(fileTypeDirectory: Bool ,fileName: String ,directory: String) ->Bool {
var filePath = applicationFilePath(fileName, directory: directory)
if fileTypeDirectory {//普通文件(图片、plist、txt等等)
return NSFileManager.defaultManager().fileExistsAtPath(filePath)
}else{//文件夹
//UnsafeMutablePointer<ObjCBool> 不能再直接使用 Bool 类型
var isDir : ObjCBool = false
return NSFileManager.defaultManager().fileExistsAtPath(filePath, isDirectory: &isDir)
}
}
// 创建文件 或 文件夹在指定路径下
func applicationCreatFileAtPath(#fileTypeDirectory: Bool ,fileName: String ,directory: String) ->Bool{
var filePath = applicationFilePath(fileName, directory: directory)
if fileTypeDirectory {//普通文件(图片、plist、txt等等)
return NSFileManager.defaultManager().createDirectoryAtPath(filePath, withIntermediateDirectories: true, attributes: nil, error: nil)
}else{//文件夹
return NSFileManager.defaultManager().createFileAtPath(filePath, contents: nil, attributes: nil)
}
}
// 移除指定路径下地文件中
func applicationRemoveFileAtPath(fileName: String ,directory: String) -> Bool{
var filePath = applicationFilePath(fileName, directory: directory)
return NSFileManager.defaultManager().removeItemAtPath(filePath, error: nil)
}
// 向指定路径下地文件中写入数据
func applicationWriteDataToFileAtPath(#dataTypeArray: Bool ,content:AnyObject ,fileName: String ,directory: String) -> Bool{
var filePath = applicationFilePath(fileName, directory: directory)
if dataTypeArray {
return (content as NSArray).writeToFile(filePath, atomically: true)
}else{
return (content as NSDictionary).writeToFile(filePath, atomically: true)
}
}
// 读取特定文件中数据(如:plist、text等)
func applicationReadDataToFileAtPath(#dataTypeArray: Bool ,fileName: String ,directory: String) -> AnyObject{
var filePath = applicationFilePath(fileName, directory: directory)
if dataTypeArray {
return NSArray(contentsOfFile: filePath)!
}else{
return NSDictionary(contentsOfFile: filePath)!
}
}
// 读取文件夹中所有子文件(如:photo文件夹中所有image)
func applicationReadFileOfDirectoryAtPath(fileName: String ,directory: String) -> AnyObject{
var filePath = applicationFilePath(fileName, directory: directory)
var content = NSFileManager.defaultManager().contentsOfDirectoryAtPath(filePath, error: nil)
return content!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}