This is sample code
func anyMethod() {
// Nothing here
}
var myVariable = ""
autoreleasepool {
anyMethod() // This should show error
print(myVariable) // This should show error
}
it should show error
Call to method 'anyMethod' in closure requires explicit 'self.' to make capture semantics explicit
But I am able to call anyMethod without self, I wonder why this not showing error
Why this is working without self ?
EDIT
Copy paste this class to re-produce
import Foundation
import UIKit
class AppGlobalManager:NSObject {
var currentUserID:String?
//Please ignore the method content as it is not the question
func appendLog(string:String?) {
print("LOG:",string)
}
func autoRelaseTest() {
autoreleasepool {
appendLog(string: "Any Log") // NO ERROR
}
}
func normalFunctionTest () {
normalFuncWithClosure {
appendLog(string: "Test") //Call to method 'appendLog' in closure requires explicit 'self.' to make capture semantics explicit
}
}
func normalFuncWithClosure (completion:@escaping() -> Void) {
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
completion()
}
}
}

Calling an instance method or referencing an instance property in a closure requires explicit
selfif that closure is passed to a function taking an@escapingparameter. That makes it explicit that theselfis possibly captured beyond the duration of the function call.If the function parameter is not
@escapingthen no explicitselfis required.Here is a self-contained example:
In your example,
Dispatch.main.asynctakes an escaping closure, butautoreleasenot.