什么是使用Swift推迟的正确方法

时间:2022-08-24 22:17:11

Swift 2.0 introduced a new keyword: defer

Swift 2.0引入了一个新的关键字:defer

What's the correct way of using this keyword, and what should I watch out for?

使用此关键字的正确方法是什么,我应该注意什么?

Since swift uses ARC, memory management is usually taken care of automagically. So defer would only need to be called for memory management for cases where using legacy low level / non arc calls, correct?

由于swift使用ARC,因此内存管理通常会自动处理。因此,对于使用传统低电平/非电弧调用的情况,只需要调用延迟来进行内存管理,对吗?

Other cases include file access, I imagine. And in these cases defer would be used for closing the "file pointer".

其他情况包括文件访问,我想。在这些情况下,延迟将用于关闭“文件指针”。

When should use I use defer in the "real-world"(tm) of iOS/OSX development. And when it would be a bad idea to use.

何时应该使用我在iOS / OSX开发的“真实世界”(tm)中使用延迟。当使用它时,这将是一个坏主意。

2 个解决方案

#1


7  

The proper use of the defer keyword is within a swift do, try, catch block. Procedures within a defer statement will always execute prior to exiting the scope of a do, try, catch block. Typically this is used for cleanup, like closing IO.

正确使用defer关键字是在swift do,try,catch块中。延迟语句中的过程将始终在退出do,try,catch块的范围之前执行。通常,这用于清理,例如关闭IO。

do {

    // will always execute before exiting scope
    defer {
        // some cleanup operation
    }

    // Try a operation that throws
    let myVar = try someThrowableOperation()

} catch {
    // error handling
}

#2


2  

defer is nice to use if you access C APIs and create CoreFoundation objects, allocate memory, or read and write files with fopen, getline. You can then make sure you clean up properly in all cases with dealloc, free, fclose.

如果您使用fopen,getline访问C API并创建CoreFoundation对象,分配内存或读取和写入文件,则defer很好用。然后,您可以确保在所有情况下使用dealloc,free,fclose正确清理。

let buffSize: Int = 1024
var buf = UnsafeMutablePointer<Int8>.alloc(buffSize)
var file = fopen ("file.txt", "w+")
defer {
  buf.dealloc(buffSize)
  fclose(file)
}
// read and write to file and and buffer down here

#1


7  

The proper use of the defer keyword is within a swift do, try, catch block. Procedures within a defer statement will always execute prior to exiting the scope of a do, try, catch block. Typically this is used for cleanup, like closing IO.

正确使用defer关键字是在swift do,try,catch块中。延迟语句中的过程将始终在退出do,try,catch块的范围之前执行。通常,这用于清理,例如关闭IO。

do {

    // will always execute before exiting scope
    defer {
        // some cleanup operation
    }

    // Try a operation that throws
    let myVar = try someThrowableOperation()

} catch {
    // error handling
}

#2


2  

defer is nice to use if you access C APIs and create CoreFoundation objects, allocate memory, or read and write files with fopen, getline. You can then make sure you clean up properly in all cases with dealloc, free, fclose.

如果您使用fopen,getline访问C API并创建CoreFoundation对象,分配内存或读取和写入文件,则defer很好用。然后,您可以确保在所有情况下使用dealloc,free,fclose正确清理。

let buffSize: Int = 1024
var buf = UnsafeMutablePointer<Int8>.alloc(buffSize)
var file = fopen ("file.txt", "w+")
defer {
  buf.dealloc(buffSize)
  fclose(file)
}
// read and write to file and and buffer down here