Câu hỏi phỏng vấn Objective-C
Câu hỏi

What happens when you invok...

Câu trả lời

Invoking a Method on a Nil Pointer in Objective-C

In Objective-C, when you invoke a method on a nil pointer, the runtime will simply ignore the message and return nil. This behavior is known as "messaging nil" and is a fundamental part of the Objective-C runtime.

Here's what happens when you invoke a method on a nil pointer:

  1. The method is not executed: The code inside the method will not be executed, and the method will return immediately.

  2. No crash or exception: Messaging nil will not cause a crash or raise an exception. The runtime simply ignores the message and continues execution.

  3. Return value is nil: If the method has a return value, it will return nil (or 0 for primitive types like int or float).

Here's an example:

objc Copy
NSString *myString = nil;
NSString *result ...
senior

senior

Gợi ý câu hỏi phỏng vấn

junior

What is the reuseIdentifier used for?

expert

What sending a message to nil means and how is it actually useful?

expert

What was wrong with the NSEnumerator technique and why NSFastEnumeration was introduced?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào