11

i am trying to avoid memory leak using clousures in swift , but swift is not recognising the [weak self] inside the closure.

  //  class :
    
    class LanguageClass {
        
        var languageClosure : (() -> ())?
        var name : String = ""
        
        init() {
            self.languageClosure = { [weak self]
                print("\(self.name) is the language")
            }
        }
        deinit {
            print("deinitialized")
        }
        
    }
    
   // consumption :

      var swiftLanguage : LanguageClass? = LanguageClass()
      swiftLanguage?.name = "Swift"
      swiftLanguage?.languageClosure?()
      swiftLanguage = nil

closure and class

2
  • 27
    [weak self] in, it's missing in.
    – Larme
    Commented Jun 22, 2021 at 11:32
  • 1
    This is not an answer -- though @Larme's comment is -- but sometimes it's because you're not in a block and therefore weak isn't possible. Commented Mar 25, 2023 at 21:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.