简体   繁体   中英

How can I make iPhone simulator generate an error when I try to dereference a nil object?

Is there any way to get the iPhone simulator to give an error or log a message when I try to access a method of a variable that is nil?

eg,

MyClass *p = nil;
[p doSomething];

No, there isn't. This is expected behavior for Objective-C, and lots of code (including Apple's frameworks) depends on it. If you somehow were to disable it, many terrible things could happen. Instead, learn to familiarize yourself with it and incorporate it in your code.

Not sure if it's what you are looking for, but zombies might be helpful. Check out:

http://iosdevelopertips.com/debugging/tracking-down-exc_bad_access-errors-with-nszombieenabled.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM