swift的代码教程-ios文件管理类(FileOp)

废话不多说,直接上码。已经经过测试。学习使用。以后持续更新swift的总结。

import UIkit
import Foundation

class FileOp{

     class func  getDocumentPath()->String{
        var path:Array = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask,true)
        return path[0] as String
    }
    
    class func  getCachesPath()->String{
        var path:Array = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.CachesDirectory,NSSearchPathDomainMask.UserDomainMask,true)
        return path[0] as String
    }
    
    class func getImageForBundle(fileName: String!)->UIImage{
        return UIImage(named:fileName)
    }
    
    class func getImageFormDecoument(fileName: String!)->UIImage{
        return UIImage(contentsOfFile: getDocumentPath()+"/"+fileName)
    }
    
    class func saveBundleImageToDoc(imageName: String!,saveImageName: String!)->Bool{
        var uniquePath:String = getDocumentPath()+"/"+saveImageName
        
        var blHave:Bool = NSFileManager.defaultManager().fileExistsAtPath(uniquePath)
        if blHave {
            var blDele:Bool = NSFileManager.defaultManager().removeItemAtPath(uniquePath, error: nil)
            if blDele {
                println("delet success")
            }else{
                println("delet erro")
                return false
            }
        }
        var arry =  imageName.componentsSeparatedByString(".")
        var path:String =  NSBundle.mainBundle().pathForResource(arry[0] as String, ofType: arry[1] as String)
        var data:NSData = NSData.dataWithContentsOfFile(path,options: NSDataReadingOptions.DataReadingMapped,error:nil )
        var result:Bool = data.writeToFile(uniquePath, atomically: true)
        return result
    }
    
    class func deletFileFromDoc(fileName: String!)->Bool{
        var blHave:Bool = NSFileManager.defaultManager().fileExistsAtPath(getDocumentPath()+"/"+fileName)
        if blHave {
            return NSFileManager.defaultManager().removeItemAtPath(getDocumentPath()+"/"+fileName, error: nil)
        }else{
            return false
        }
    }
    
    class func saveDataPlistToDoc(plistName:String!,listData:NSMutableDictionary!)->Bool{
        var result:Bool =  listData.writeToFile(getDocumentPath()+"/"+plistName, atomically: true)
        return result;
    }
    
    
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值