Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursive issue with getter ios objective c

I have this property:

@property (nonatomic, getter = getSolutionsCount, setter = setSolutionsCount:) NSInteger solutionsCount;

and implementation

- (NSInteger)getSolutionsCount {
    return self.solutionsCount;
}

and I get EXC_BAD_ACCESS on this method - (NSInteger)getSolutionsCount.

What am I doing wrong here?

like image 517
Matrosov Oleksandr Avatar asked Feb 19 '26 03:02

Matrosov Oleksandr


2 Answers

dot syntax is basically a shortcut for calling the getter. You have infinite recursion in your getter method.

What you need to do is return the instance variable directly:

- (NSInteger)getSolutionsCount {
    //latest xcode makes variables with _property name automatically
    return _solutionsCount;

    //older versions of xcode or having written @synthesize solutionsCount
    //will necessitate
    //return solutionsCount;
}

Also just FYI objective-c convention is to have the getter method be defined as just the variable name. A getter which is the same as the property name is assumed if you don't write a getter in the property declaration

EDIT: also i'm assuming this isnt the whole implementation for your getter because if it is let the compiler make it for you automatically, you don't need to write anything. (or by writing @synthesize propertyName = _propertyName in your implementation block with older versions of xCode)

like image 172
jackslash Avatar answered Feb 21 '26 17:02

jackslash


The line self.solutionsCount is translated to [self getSolutionCount]. You are making a recursive call.

If you simply want to return the synthesized ivar then don't even implement this method. But if you do then simply call return _solutionCount;.

like image 21
rmaddy Avatar answered Feb 21 '26 18:02

rmaddy