Friday 15 March 2013

objective c - Why can we access a property of a nil NSObject without an exception? -


I have this method that I put an object and it updates a view based on that object. However, even when this object is zero, this crash does not happen even when I am not defensive.

  - (zero) UpdateWidth object: (NSObject *) obj {// obj is not so I use the property with the exception? If ([[NSDrmument *] [OBG property] valueForKey: @ "KEY"]) {// Set object object value} Set other {// object values}}  

This only works the purpose - you send messages to any object to reach the properties or call methods. To do this, the C function objc_msgSend is selected with the object, the selector and all other logic of the method. It just does not call the method before it checks that the receiver is zero, if it happens then returns immediately with the result of zero (or zero or empty straight), otherwise it looks at the appropriate method for the selector and if found So call. If this message does not enter the forwarding machinery which sends some other message to this object so that it can handle the unknown selector dynamically. All this is done without throwing exceptions or otherwise crashing. Only the last resort is called forwarding: whose default implementation throws an exception in NSObject and crashes in such a way before the object can handle this message There were plenty of possibilities.


No comments:

Post a Comment