I’m new to Objective-C and and I can’t understand this. I know that I can send a message to nil (it’s so hyped about feature of Objective-C), but I can’t send a message to released object, getting an exception in this case, what the difference between them?
Share
nilis the memory address 0. The runtime knows to not do anything when this address is messaged, because it is the predefined nonexistant object address.However, a deallocated object will a random memory address, because the pointer isn’t cleaned up when the formerly valid object is destroyed. Since it is not the prescribed nonexistant object address, the runtime doesn’t know that it’s invalid, and will try to send it the message. This will usually crash your program right away.
You can avoid this by setting variables to
nilonce you’ve released them.