Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 正在从共享应用程序组中删除文件_Swift_Ios App Group - Fatal编程技术网

Swift 正在从共享应用程序组中删除文件

Swift 正在从共享应用程序组中删除文件,swift,ios-app-group,Swift,Ios App Group,我有一个将文件成功移动到共享应用程序组的功能,但我删除该文件的功能似乎不起作用。如果我打印出fullpath2变量,它看起来是正确的位置,但是文件没有被删除,也没有返回错误 以下是我的功能: func getSharedFilePath(appGroup:String,sharedFilename:String)->URL? { if let directoryPath = FileManager().containerURL(forSecurityApplicationGroupIde

我有一个将文件成功移动到共享应用程序组的功能,但我删除该文件的功能似乎不起作用。如果我打印出fullpath2变量,它看起来是正确的位置,但是文件没有被删除,也没有返回错误

以下是我的功能:

func getSharedFilePath(appGroup:String,sharedFilename:String)->URL? {

if let directoryPath = FileManager().containerURL(forSecurityApplicationGroupIdentifier: appGroup) {
    return directoryPath.appendingPathComponent(sharedFilename)
} else {
    return nil
}
}


public func deleteFromSharedFile(sharedFilename: String, fileExtension: String)->String {
let sharedFilename = "\(sharedFilename).\(fileExtension)"
guard let url = getSharedFilePath(appGroup:applicationGroup,sharedFilename:sharedFilename) else {
    return("Error getting shared file path")
}

// read file from file system to data variable
let fileManager = FileManager.default
do {
    try fileManager.removeItem(atPath: (url.path))
    return("File Removed")
}
catch let error as NSError {
    return("File Remove Failed - \(error)")
}
}

下面是我的表情应用程序的一些代码片段

它工作得很好

func removeImage(itemName: String, fileExtension: String) {
    let fileName:String = itemName + "." + fileExtension
    let fileManager = FileManager.default
    guard let groupURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.io.XXXXXX.XXXX") else {
        return
    }
    let storagePathUrl = groupURL.appendingPathComponent("image/" + fileName, isDirectory: false)
    let storagePath = storagePathUrl.path

    do {
       if fileManager.fileExists(atPath: storagePath) {
           try fileManager.removeItem(atPath: storagePath)
        }
    } catch let error as NSError {
        print(error.debugDescription)
    }
}
“图像”只是应用程序组子文件夹的名称

如果直接将文件保存在根文件夹中,则可以将其删除


希望对您有所帮助。

以下是我的表情应用程序的一些代码片段

它工作得很好

func removeImage(itemName: String, fileExtension: String) {
    let fileName:String = itemName + "." + fileExtension
    let fileManager = FileManager.default
    guard let groupURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: "group.io.XXXXXX.XXXX") else {
        return
    }
    let storagePathUrl = groupURL.appendingPathComponent("image/" + fileName, isDirectory: false)
    let storagePath = storagePathUrl.path

    do {
       if fileManager.fileExists(atPath: storagePath) {
           try fileManager.removeItem(atPath: storagePath)
        }
    } catch let error as NSError {
        print(error.debugDescription)
    }
}
“图像”只是应用程序组子文件夹的名称

如果直接将文件保存在根文件夹中,则可以将其删除


希望它能帮助您。

url.absoluteString
错误。你需要找到它的路径
尝试fileManager.removietem(atPath:url.path)
。顺便说一句,absoluteString还返回url方案(本例中为文件://),您将在下一行添加并删除该方案。@Leodbus使用url.path无效。请用实际值更新您的问题code@LeoDabus很抱歉,我已经更新了您需要的问题,并发布了如何获取文件URL。错误是什么?
url.absoluteString
错误。你需要找到它的路径
尝试fileManager.removietem(atPath:url.path)
。顺便说一句,absoluteString还返回url方案(本例中为文件://),您将在下一行添加并删除该方案。@Leodbus使用url.path无效。请用实际值更新您的问题code@LeoDabus很抱歉,我已经更新了您需要的问题,并发布了如何获取文件URL。错误是什么?