Weak reference

PHOTO EMBED

Fri Apr 12 2024 06:37:48 GMT+0000 (Coordinated Universal Time)

Saved by @Saurabh_Lodhi #swift #retailcycle

//'weak' must not be applied to non-class-bound 'any ListNotesDelegate'; consider adding a protocol conformance that has a class bound

//Whenever we want to create a weak reference the parent of object must conform to a class like AnyObject or UILabel etc.

//Example - 1

@IBOutlet weak private var titleLbl: UILabel!  //Here UILabel is a class
  
//Example - 2

protocol ListNotesDelegate {
    func refreshNotes()
    func deleteNote(with id: UUID)
}  

var delegate: ListNotesDelegate?  //You cannot assign this as weak var 
  
//To define it as weak -

protocol ListNotesDelegate:AnyObject {//Here AnyObject is a type alias for multiple classes
    func refreshNotes()
    func deleteNote(with id: UUID)
}  

weak var delegate: ListNotesDelegate?
content_copyCOPY