在闭包内部使用函数参数

时间:2023-01-02 16:45:13

I just want to use function parameter inside swift closure without memory leak, so I just want to confirm if I do in following way will there be any memory related issues? Kindly let me know

我只是想在swift闭包内使用函数参数,没有内存泄漏,所以我只是想确认一下,如果我这样做,会不会有内存相关的问题?请让我知道

func someMethod(someValue: String) {
    weak var weakSelf = self
    var copyOfSomeValue: String? = someValue.copy() as? String
    self.someOtherMethodWithCompletion(completionHandler: { () -> Void in
        if let strongSelf = weakSelf, let originalValue = copyOfSomeValue {
            strongSelf.updateMyViewWithText(originalValue)
        }
    })
}

2 个解决方案

#1


1  

String is a value type. Even though closures capture them by reference, there is no need to make a copy unless you are going to change someValue after the capture. Even then you're better off using capture lists [someValue], which are also used when you need to declare [weak self].

字符串是一种值类型。即使闭包通过引用捕获它们,也不需要复制,除非您打算在捕获之后更改someValue。即便如此,最好还是使用捕获列表[someValue],当您需要声明[弱自我]时也使用该列表。

Using weak or unowned is situational, read about them here.

使用弱的或非拥有的都是情境性的,请阅读这里。

func someMethod(someValue: String) {
    someOtherMethodWithCompletion { [weak self] in
        self?.updateMyViewWithText(someValue)
    }
}

#2


1  

With swift you should use [unowned self] and just use swift as normal, for example:

使用swift时,您应该使用[非拥有的自己],并使用swift作为正常,例如:

func someMethod(someValue: String) {
    var copyOfSomeValue: String? = someValue.copy() as? String
    self.someOtherMethodWithCompletion(completionHandler: { () ->
        [unowned self]
         in
        if let originalValue = copyOfSomeValue {
            self.updateMyViewWithText(originalValue)
        }
    })
}

You should use weak or unowned just to variables which can cause reference cycle. The difference between those is:

您应该只对可能导致引用周期的变量使用弱变量或非拥有变量。两者的区别是:

Use a weak reference whenever it is valid for that reference to become nil at some point during its lifetime. Conversely, use an unowned reference when you know that the reference will never be nil once it has been set during initialization

当弱引用在其生命周期中的某个时刻对该引用有效时,请使用弱引用。相反,当您知道引用在初始化过程中被设置后永远不会为nil时,可以使用非所有引用

#1


1  

String is a value type. Even though closures capture them by reference, there is no need to make a copy unless you are going to change someValue after the capture. Even then you're better off using capture lists [someValue], which are also used when you need to declare [weak self].

字符串是一种值类型。即使闭包通过引用捕获它们,也不需要复制,除非您打算在捕获之后更改someValue。即便如此,最好还是使用捕获列表[someValue],当您需要声明[弱自我]时也使用该列表。

Using weak or unowned is situational, read about them here.

使用弱的或非拥有的都是情境性的,请阅读这里。

func someMethod(someValue: String) {
    someOtherMethodWithCompletion { [weak self] in
        self?.updateMyViewWithText(someValue)
    }
}

#2


1  

With swift you should use [unowned self] and just use swift as normal, for example:

使用swift时,您应该使用[非拥有的自己],并使用swift作为正常,例如:

func someMethod(someValue: String) {
    var copyOfSomeValue: String? = someValue.copy() as? String
    self.someOtherMethodWithCompletion(completionHandler: { () ->
        [unowned self]
         in
        if let originalValue = copyOfSomeValue {
            self.updateMyViewWithText(originalValue)
        }
    })
}

You should use weak or unowned just to variables which can cause reference cycle. The difference between those is:

您应该只对可能导致引用周期的变量使用弱变量或非拥有变量。两者的区别是:

Use a weak reference whenever it is valid for that reference to become nil at some point during its lifetime. Conversely, use an unowned reference when you know that the reference will never be nil once it has been set during initialization

当弱引用在其生命周期中的某个时刻对该引用有效时,请使用弱引用。相反,当您知道引用在初始化过程中被设置后永远不会为nil时,可以使用非所有引用