美文网首页
IOS 复制bundle 资源到Document

IOS 复制bundle 资源到Document

作者: 假装门口当前台 | 来源:发表于2019-12-26 14:40 被阅读0次

Bundle可以说是在打包之后可以存储一些资源,有时候需要把bundle路径下的某些资源拷贝到Document中,方便文件的修改,以下是用到的一些方法
获取Bundle 的主路径

let path = Bundle.main.resourcePath ?? ""

获取Document路径

let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last ?? ""

如果需要在改路径下新建子目录,只需要在后面拼接 "/xxxx" 即可

删除某路径文件或者文件夹

FileManager.default.removeItem(atPath: filePath)

判断文件或者文件夹是否存在

 FileManager.default.fileExists(atPath: path)

判断是否是文件夹

  var isDir:ObjCBool = ObjCBool(false)
  FileManager.default.fileExists(atPath: tmp, isDirectory: &isDir)
isDir为true就是文件夹

复制文件或者文件夹,很简单,就一句话

FileManager.default .copyItem(atPath: path, toPath: filePath)

完整代码

        let path = (Bundle.main.resourcePath ?? "") + "/myDir"
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last ?? ""
        print("docDir = " + docDir)

        let filePath = docDir + "/myDir"

        if YS_DEBUG || UserDefaults.standard.integer(forKey: LUA_VERSION_KEY) < version {
            try?FileManager.default.removeItem(atPath: filePath)
        }
        
       
        if !FileManager.default.fileExists(atPath: filePath, isDirectory: .none) {
            try? FileManager.default .copyItem(atPath: path, toPath: filePath)
            UserDefaults.standard.set(version, forKey: LUA_VERSION_KEY)
        } else {
        }

相关文章

网友评论

      本文标题:IOS 复制bundle 资源到Document

      本文链接:https://www.haomeiwen.com/subject/srokoctx.html