swift iOS: 在应用文档文件夹中创建 UIDocument
swift iOS: create UIDocument in app documents folder
我正在寻找一种方法。我说选项如何处理已经创建但 UIDocument 不能仅从 URL.
初始化的文件
所以,这是我的代码,但它不起作用:
weak var weakSelf = self
let toFolder = presenter.interactor.url.path
let name = "Untitled"
var toPath = toFolder + "/" + name
var count = 1
while FileManager.default.fileExists(atPath: toPath) {
toPath = toFolder + "/" + name + " (\(count))"
count += 1
}
let url = (toPath + ".UTI").url!
print(url.absoluteString)
let document = Document(fileURL: url)
document.save(to: url, for: UIDocument.SaveOperation.forCreating, completionHandler: { (success) in
if success {
vc.document = document
vc.title = url.lastPathComponent
let nvc = UINavigationController(rootViewController: vc)
weakSelf?.userInterface.present(nvc, animated: true, completion: nil)
} else {
Alert.showError(body: "Could not create document".localized)
}
})
url 是:
/var/mobile/Containers/Data/Application/67C2A474-A054-4DFE-8587-B453A8B44554/Documents/Untitled.UTI
我在字符串上崩溃了:"let document = Document(fileURL: url)"
*** 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'must pass a valid file URL to -[UIDocument initWithFileURL:]'
class 文件:
enum DocumentError: Error {
case saveError
case loadError
}
class Document: UIDocument {
var text: String?
var encodingUsed: String.Encoding?
override func contents(forType typeName: String) throws -> Any {
guard let data = text?.data(using: .utf8) else {
throw DocumentError.saveError
}
return data
}
override func load(fromContents contents: Any, ofType typeName: String?) throws {
guard let data = contents as? Data else {
throw DocumentError.loadError
}
guard let utf8 = String(data: data, encoding: .utf8) else {
throw DocumentError.loadError
}
self.text = utf8
}
}
var url: URL? {
if self != "" && !isEmpty {
return URL(string: self)
} else {
return nil
}
}
您的扩展名应该 return:URL(fileURLWithPath: self)
而不是 URL(string: self)
因为这是本地文件。
您创建路径和 URLs 的代码需要大量工作。但最后你需要使用 URL(fileURLWithPath:)
创建一个文件 URL 并将其传递给你的 Document
.
let toFolder = presenter.interactor.url
let name = "Untitled"
var toPath = toFolder.appendingPathComponent(name)
var count = 1
while FileManager.default.fileExists(atPath: toPath.path) {
toPath = toFolder.appendingPathComponent(name + " (\(count))")
count += 1
}
let url = toPath.appendingPathExtension("UTI")
print(url.path)
let document = Document(fileURL: url)
我正在寻找一种方法。我说选项如何处理已经创建但 UIDocument 不能仅从 URL.
初始化的文件所以,这是我的代码,但它不起作用:
weak var weakSelf = self
let toFolder = presenter.interactor.url.path
let name = "Untitled"
var toPath = toFolder + "/" + name
var count = 1
while FileManager.default.fileExists(atPath: toPath) {
toPath = toFolder + "/" + name + " (\(count))"
count += 1
}
let url = (toPath + ".UTI").url!
print(url.absoluteString)
let document = Document(fileURL: url)
document.save(to: url, for: UIDocument.SaveOperation.forCreating, completionHandler: { (success) in
if success {
vc.document = document
vc.title = url.lastPathComponent
let nvc = UINavigationController(rootViewController: vc)
weakSelf?.userInterface.present(nvc, animated: true, completion: nil)
} else {
Alert.showError(body: "Could not create document".localized)
}
})
url 是:
/var/mobile/Containers/Data/Application/67C2A474-A054-4DFE-8587-B453A8B44554/Documents/Untitled.UTI
我在字符串上崩溃了:"let document = Document(fileURL: url)"
*** 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:'must pass a valid file URL to -[UIDocument initWithFileURL:]'
class 文件:
enum DocumentError: Error {
case saveError
case loadError
}
class Document: UIDocument {
var text: String?
var encodingUsed: String.Encoding?
override func contents(forType typeName: String) throws -> Any {
guard let data = text?.data(using: .utf8) else {
throw DocumentError.saveError
}
return data
}
override func load(fromContents contents: Any, ofType typeName: String?) throws {
guard let data = contents as? Data else {
throw DocumentError.loadError
}
guard let utf8 = String(data: data, encoding: .utf8) else {
throw DocumentError.loadError
}
self.text = utf8
}
}
var url: URL? {
if self != "" && !isEmpty {
return URL(string: self)
} else {
return nil
}
}
您的扩展名应该 return:URL(fileURLWithPath: self)
而不是 URL(string: self)
因为这是本地文件。
您创建路径和 URLs 的代码需要大量工作。但最后你需要使用 URL(fileURLWithPath:)
创建一个文件 URL 并将其传递给你的 Document
.
let toFolder = presenter.interactor.url
let name = "Untitled"
var toPath = toFolder.appendingPathComponent(name)
var count = 1
while FileManager.default.fileExists(atPath: toPath.path) {
toPath = toFolder.appendingPathComponent(name + " (\(count))")
count += 1
}
let url = toPath.appendingPathExtension("UTI")
print(url.path)
let document = Document(fileURL: url)