Objective-C Getter / Setter Error on EXC_BAD_ACCESS Property

I was wondering why assigning values โ€‹โ€‹to the properties of another class calls EXC_BAD_ACCESS. I canโ€™t understand why.

1.) The value sent to the setter is not zero; 2.) When trying to assign the value EXC_BAD_ACCESS, the variable is nil;

Using this template in both Cocoa and Cocoa Application Touch calls EXC_BAD_ACCESS, so I donโ€™t think it is a platform, but I think it is a template that I use.

My questions are: is this when variables are assigned, or is this how I create properties?

I created a dummy project, which is shown in the figures below.

Any help is appreciated.

enter image description here

enter image description here

EDIT: After doing some copying, I changed the name of the setter variable (and not the property name) to firstName__. Basically, the code in setter for setFirstName:

- (void)setFirstName:(NSString *)firstName__ { self.firstName = firstName__; } 

Doing this clarified a bit of confusion by saying that firstName__ (not self.firstName) is nil, although in AppDelegate the value is not nil.

+5
source share
1 answer

Your problem is recursion. In the installer, you call the setter method over and over and over. When you announce

 self.firstName = first name__; 

basically equivalent

 [self setFirstName:first name__]; 

Thus, the method calls the call itself, which does not make much sense.

First you need to wrap your head around properties and instance variables.

Class C object instances often contain instance variables that contain values. Although these variables can be exposed to the outside world through the @public classifier, this is not an established convention. The convention is to have properties that are behind the scenes a "wrapper" around a private instance variable. Since in Objective-C you can only communicate with other objects through messages in order to access the values โ€‹โ€‹of the instance variable, you have setter and getter methods that are called when the corresponding message is sent to this object.

The modern target C creates an instance variable for you implicitly when declaring properties. It is often said that these properties are supported by instance variables.

Usually there is no reason to explicitly implement setters and getters, since the compiler does this behind the scenes. (in the same way, it also creates these instance variables for you)

But if you want to explicitly implement setters, you need to set the instance variable in the customizer, and not call the setter itself (via the dot symbol), as I explained above.

Implicitly created instance variables have a naming convention with an underscore as a prefix. In your case, this is

 _firstName 

When you declare a property called firstName, you also get an instance variable
_firstName

You should look like this:

 -(void)setFirstName:(NSString *)firstName { _firstName = firstName; } 

And getter

 -(NSstring *)getFirstName { return _firstName; } 
+11
source

Source: https://habr.com/ru/post/1213834/


All Articles