0

我是 Xcode 和 Swift 的新手。目前正在编写处理 Apple 的 ODR 的脚本: https ://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/On_Demand_Resources_Guide/Managing.html#//apple_ref/doc/uid/TP40015083-CH4-SW1

我使用 NSBundleResourceRequest 作为

assetsPack = NSBundleResourceRequest(tags: [tag])

而不是通过给定标签从我的 Assets.xcassets 下载特定资源: conditionallyBeginAccessingResources实例方法。

我的主要代码片段是:

var assetsPack: NSBundleResourceRequest?

 if let req = assetsPack{
    req.assetsPack()
  }
  
  assetsPack = NSBundleResourceRequest(tags: [tag])
    
   
  guard let req = assetsPack else {
    return
  }
  

  req.conditionallyBeginAccessingResources{available in
    if available{
        
        print("available")
        print(available)
      
        self.anotherFunction(tag)
    } else {
      req.beginAccessingResources{error in
        guard error == nil else{
          return
        }
        self.anotherFunction(tag)
      }
    }

我需要的是在此处返回 ODR 资源的路径或将其传递给另一个函数。我需要能够并使用此路径将我的文件复制到另一个地方,或者在使用另一个外部插件下载后访问它。

我一直在尝试一些方法,例如:

    let path = req.bundle.url(forResource: "myFile", withExtension: "data")
    print(path)

考虑到我的 Assets.xcassets 中有一个 myFile 类型的数据。

但它返回零。

我也试过:

让 stringPath = Bundle.main.path(forResource: "myFile", ofType: "data")

让 urlPath = Bundle.main.url(forResource: "myFile", withExtension: "data")

4

0 回答 0