Crash when unloading ItemDetailViewController


#1

I get a crash when hitting the camera button, which triggers the ItemDetailViewController’s viewDidUnload: method. So I traced the crash to the setting of the properties such as (nameField) to nil. In the book, the properties are set to nil after being released like this:

nameField = nil;

However, the code generated by control dragging to create the outlets produced this instead:

[self setNameField:nil]

This difference causes a crash. Reverting from the latter to the former stops the crash. So my question is, what is the difference between the two methods of setting the instance variable to nil, and why does the latter cause a crash?


#2

I’m pretty new at this but I’m guessing you didn’t create setters or getters for nameField.


#3

I don’t think so. Setters and getters are automatically created by @synthesize.

My point is that the auto-generated code created by control-dragging in my version of Xcode is different that what is in the book. This difference causes a crash. I don’t understand why calling a setter for a synthesized property would cause a crash, but directly changing the instance variable does not.


#4

Hmm, I only mention it because when I look at my code from the book, I never did anything with nameField but declare it as a variable in my header file. If I try to use [self setNameField:nil]; I get a warning saying that method can’t be found.

If, however, I add @property (nonatomic, retain) UITextField *nameField; to my header file, and @synthesize nameField; to my implementation, then, like you said, the setter is automatically created and I can use [self setNameField:nil];