0

I ran into this issue:

I defined a enum type in the header file:

typedef enum {
    aView,
    bView,
    cView
} SCViewType;

and then in my header file, I have:

@property (nonatomic) SCViewType currentView;

In my .m file, I have this:

- (void) setCurrentView: (SCViewType) view
{
    self.currentView = view;
}

And this self.currentView = view line got BAD_ACCESS error...

Please point me what's wrong with this implementation, and what I should do to fix it?

Thank you!

Allan Jiang
  • 11,063
  • 27
  • 104
  • 165
  • Is that all you do in your `setCurrentView:` method? If so, why override the synthesized setter method for the property? – rmaddy Jul 29 '14 at 03:41

2 Answers2

0

When you call self.currentView = view;. Objective-C will convert to - (void) setCurrentView: (SCViewType) view automatically. So this method will be called again and again.

Just modify the code to:

- (void) setCurrentView: (SCViewType) view
{
    _currentView = view;
}
Louis Zhu
  • 792
  • 5
  • 12
  • Keep in mind that of the OP also implements the getter method, the ivar won't exist unless it is added explicitly. – rmaddy Jul 29 '14 at 03:41
0

The "self." will call the the setter method and end up in endless loop. More detail

Community
  • 1
  • 1
Eric
  • 1
  • 2