Ios 在swift&x27中完成关闭;他似乎没有开枪
我正在阅读O'reilly的《学习Swift》一书,我有一些代码如下所示:Ios 在swift&x27中完成关闭;他似乎没有开枪,ios,swift,Ios,Swift,我正在阅读O'reilly的《学习Swift》一书,我有一些代码如下所示: func deleteDocumentAtURL(url: NSURL) { NSLog("Got to top of deleteDocumentAtURL, url: \(url)") let fileCoordinator = NSFileCoordinator(filePresenter: nil) fileCoordinator.coordinateWritingItemAtURL(ur
func deleteDocumentAtURL(url: NSURL) {
NSLog("Got to top of deleteDocumentAtURL, url: \(url)")
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
fileCoordinator.coordinateWritingItemAtURL(url, options: .ForDeleting, error: nil, byAccessor: { (urlForModifying) -> Void in
NSLog("Here I am")
do {
NSLog("Got inside deleteDocumentBlock")
try NSFileManager.defaultManager().removeItemAtURL(urlForModifying)
// Remove the URL from the list
self.availableFiles = self.availableFiles.filter {
$0 != url
}
// Update the collection
self.collectionView?.reloadData()
} catch let error as NSError {
let alert = UIAlertController(title: "Error deleting", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Done", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})
}
当我运行此代码时(通过单击界面中的按钮触发),应用程序将挂起。另外,do{}内的日志没有触发,这让我觉得整个块有问题?感谢您的帮助。因为
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
在函数中是本地的。如果您完成了该函数,则该变量已被销毁。
因此,无法执行异步块
将fileCoordinator作为实例变量添加到类中。因为
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
在函数中是本地的。如果您完成了该函数,则该变量已被销毁。
因此,无法执行异步块
将fileCoordinator作为实例变量添加到类中。您没有处理错误,可能有错误 : outError:在输入时,指向错误对象指针的指针。如果文件演示者在准备此读取操作时遇到错误,此参数将返回该错误,并且不会执行读卡器参数中的块。如果在执行读卡器块之前取消此操作,则此参数在输出时包含错误对象 添加错误处理程序并查看是否出现错误
func deleteDocumentAtURL(url: NSURL) {
NSLog("Got to top of deleteDocumentAtURL, url: \(url)")
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
var error: NSError?
fileCoordinator.coordinateWritingItemAtURL(url, options: .ForDeleting, error: &error, byAccessor: { (urlForModifying) -> Void in
NSLog("Here I am")
do {
NSLog("Got inside deleteDocumentBlock")
try NSFileManager.defaultManager().removeItemAtURL(urlForModifying)
// Remove the URL from the list
self.availableFiles = self.availableFiles.filter {
$0 != url
}
// Update the collection
self.collectionView?.reloadData()
} catch let error as NSError {
let alert = UIAlertController(title: "Error deleting", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Done", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})
//Error:
if(error != nil){
print(error)
}
}
您没有处理错误,可能有错误 : outError:在输入时,指向错误对象指针的指针。如果文件演示者在准备此读取操作时遇到错误,此参数将返回该错误,并且不会执行读卡器参数中的块。如果在执行读卡器块之前取消此操作,则此参数在输出时包含错误对象 添加错误处理程序并查看是否出现错误
func deleteDocumentAtURL(url: NSURL) {
NSLog("Got to top of deleteDocumentAtURL, url: \(url)")
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
var error: NSError?
fileCoordinator.coordinateWritingItemAtURL(url, options: .ForDeleting, error: &error, byAccessor: { (urlForModifying) -> Void in
NSLog("Here I am")
do {
NSLog("Got inside deleteDocumentBlock")
try NSFileManager.defaultManager().removeItemAtURL(urlForModifying)
// Remove the URL from the list
self.availableFiles = self.availableFiles.filter {
$0 != url
}
// Update the collection
self.collectionView?.reloadData()
} catch let error as NSError {
let alert = UIAlertController(title: "Error deleting", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Done", style: .Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
})
//Error:
if(error != nil){
print(error)
}
}
那么,另一个应用程序也在访问文档吗?@jtbandes不应该是这样的吗?我怎么说呢?还有,为什么这会阻止我进入这个区块呢?因为这就是NSFileCoordinator的设计目的。那么,另一个应用程序也在访问文档吗?@jtbandes不应该是这样的?我怎么说呢?还有,为什么这会阻止我进入这个区块呢?因为这就是NSFileCoordinator的设计目的。不。closure保存所有相关的references\value,直到它被销毁<代码>闭包可以从定义它们的上下文中捕获并存储对任何常量和变量的引用。这被称为关闭这些常量和变量。Swift为您处理捕获的所有内存管理。No.closure会保存所有相关引用\值,直到它被销毁<代码>闭包可以从定义它们的上下文中捕获并存储对任何常量和变量的引用。这被称为关闭这些常量和变量。Swift为您处理捕获的所有内存管理。这个答案是有道理的,但是当我试图将它应用到我的代码中时,xcode变得不高兴,并抛出了一个关于分隔符的错误。有没有想过我做错了什么?@phildini你是说它不会编译?请确保我唯一的更改是添加
var error:NSError?
,并确保将其作为&error
发送到函数。因此,仅添加这些内容即可使其编译,但我仍然会收到错误信息:/@phildini嗯,这就是问题所在,块没有被执行,因为函数现在看起来是这样的:这个答案是有道理的,但是当我试图将它应用到我的代码中时,xcode变得不高兴,并抛出了一个关于分隔符的错误。有没有想过我做错了什么?@phildini你是说它不会编译?请确保我唯一的更改是添加var error:NSError?
,并确保将其作为&error
发送到函数。因此,仅添加这些内容即可使其编译,但我仍然会收到错误信息:/@phildini好吧,这就是问题所在,块没有被执行,因为函数现在看起来是这样的: