10

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
  • 24
    [weak self] in, it's missing in.
    – Larme
    Jun 22, 2021 at 11:32
  • 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. Mar 25 at 21:38

0

Your Answer

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

Browse other questions tagged or ask your own question.