Setting delegate to nil in dealloc
In Objective-C, I understand that if an object sets itself as the delegate of another object, it should set that object’s delegate to
nil in its
dealloc to avoid getting sent messages after it’s gone.
However, when using Accessorizer (an Xcode utility), the stub code it generates also puts a
delegate = nil in the
dealloc of the class that has the delegate instance variable. What is the purpose of that?
Solutions Collect From Internet About “Setting delegate to nil in dealloc”
It’s a defensive programming move. It’s clearing out the reference to the delegate object incase something else in your object tries to access the delegate after you’ve told it that you’re done with it. As part of your dealloc you might have a method or do something that triggers a KVO notification that makes a call to the delegate. So setting the delegate’s reference to nil prevents that from happening. If it did happen you could end up with some oddball crashes that are fun to reproduce and fix.
- Saving multiple objects in Parse taking more than 1 API request?
- Push notifications aren't send to some devices
- Set values on TableView Swift 3 on function
- iOS 8 MKAnnotationView rightCalloutAccessoryView misaligned
- Screenshot app that can take screenshots in any view
- How to forward functions with variadic parameters?
- Animation Snaps back when the UIImage is changed
- NSInternalInconsistencyException: 'Invalid parameter not satisfying: !stayUp || CLClientIsBackgroundable(internal->fClient)'
- Read file in swift, iOS playground
- How to search the iphone address book for a specific phone number?
- MPVolumeView does not show route button on launch
- linguistic meaning of 'let' variable in programming
- How to declare a two dimensional array of string type in Objective-C?
- Override a method in Objective c via category
- AVAudioEngine playing multi channel audio