Swift 中的 throw 是如何通过 'startAccessingSecurityScopedResource()' 声明的?
How does throw declare throughs 'startAccessingSecurityScopedResource()' in Swift?
我会通过当前文件选择器导入数据。我正在查看开发文档并按照它进行操作,但有一个问题。当我不在守卫的条件语句中时,我即将出错,但是'throw
'不可用。你如何解决这个问题?
func startAccessingSecurityScopedResource() -> Bool
用法
func presentDocument(at documentURL: URL) {
// Start accessing a security-scoped resource.
guard documentURL.startAccessingSecurityScopedResource() else {
throw IXError.fileAcessFailed // Error is not handled because the enclosing function is not declared 'throws'
return
}
Error is not handled because the enclosing function is not declared
'throws'
我尝试了 guard let
但失败了。
guard let Acess = documentURL.startAccessingSecurityScopedResource() else { //Initializer for conditional binding must have Optional type, not 'Bool'
throw IXError.fileAcessFailed // Error is not handled because the enclosing function is not declared 'throws'
return
}
IXError
public enum IXError: Error {
...
}
*********************************编辑开始 *********** *********************************
我按照答案添加了throws
,但是使用它的函数出现了错误
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
guard let sourceURL = documentURLs.first else { return }
presentDocument(at: sourceURL) // Call can throw, but it is not marked with 'try' and the error is not handled
}
...
func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
presentDocument(at: destinationURL) // Call can throw, but it is not marked with 'try' and the error is not handled
}
...
func presentDocument(at documentURL: URL) throws {
guard documentURL.startAccessingSecurityScopedResource() else {
throw IXError.fileAcessFailed
return
}
Error is Call can throw, but it is not marked with 'try' and the error
is not handled
Error is not handled because the enclosing function is not declared 'throws'
这指的是您的 presentDocument
函数。像这样声明:
func presentDocument(at documentURL: URL) throws {
...
}
我会通过当前文件选择器导入数据。我正在查看开发文档并按照它进行操作,但有一个问题。当我不在守卫的条件语句中时,我即将出错,但是'throw
'不可用。你如何解决这个问题?
func startAccessingSecurityScopedResource() -> Bool
用法
func presentDocument(at documentURL: URL) {
// Start accessing a security-scoped resource.
guard documentURL.startAccessingSecurityScopedResource() else {
throw IXError.fileAcessFailed // Error is not handled because the enclosing function is not declared 'throws'
return
}
Error is not handled because the enclosing function is not declared 'throws'
我尝试了 guard let
但失败了。
guard let Acess = documentURL.startAccessingSecurityScopedResource() else { //Initializer for conditional binding must have Optional type, not 'Bool'
throw IXError.fileAcessFailed // Error is not handled because the enclosing function is not declared 'throws'
return
}
IXError
public enum IXError: Error {
...
}
*********************************编辑开始 *********** *********************************
我按照答案添加了throws
,但是使用它的函数出现了错误
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL]) {
guard let sourceURL = documentURLs.first else { return }
presentDocument(at: sourceURL) // Call can throw, but it is not marked with 'try' and the error is not handled
}
...
func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
presentDocument(at: destinationURL) // Call can throw, but it is not marked with 'try' and the error is not handled
}
...
func presentDocument(at documentURL: URL) throws {
guard documentURL.startAccessingSecurityScopedResource() else {
throw IXError.fileAcessFailed
return
}
Error is Call can throw, but it is not marked with 'try' and the error is not handled
Error is not handled because the enclosing function is not declared 'throws'
这指的是您的 presentDocument
函数。像这样声明:
func presentDocument(at documentURL: URL) throws {
...
}