My friend discovered some strange behavior with NSDictionary, and I'm curious why this is happening. Consider the following code:
NSDictionary *dict = [[NSDictionary alloc] init];
The code generates a compilation warning that "NSDictionary" may not respond to "setObject: forKey:". All this is good and good, and if you run it anyway, you will get this output in the console:
- [__ NSCFDictionary setObject: forKey:]: mutation method sent to an immutable object
Again, exactly what you expect. However, at the moment, the application is not crashing or terminating due to an uncaught exception. The setObject: forKey: method simply never returns, and the application seems to hang; The next NSLog never executed. If you try to step over or use a method using GDB, debugging will simply end, but without an explicit error message. The application continues to work, but the debugger does not give any idea of ββwhere the execution is "stuck" in the code.
What's going on here? What does the application really do in this case, and why doesn't it crash with an NSInternalInconsistencyException or something like that?
Change For those who asked, I run Xcode 4.1 on OS X Lion (10.7.2), creating the "Apple LLVM Compiler 2.1". I use all the default settings that you get with the new Cocoa project in XCode 4. I experience the same behavior without crashing, regardless of whether I debug the program or just βRunβ it. The transition from the Debug building to the release of the building does not matter. I can even find the .app file manually in Finder and double-click on it to execute it outside of Xcode, and it still doesn't crash.
source share