1

Firstly, I wrote my customized setter for an NSString* like this:

- (void)setDateString:(NSString *)newDateString {
    self.dateString = newDateString;
    NSInteger dateNumber = [dateString integerValue];
        // this line causes crash

    // do something here..blah blah
}

then the program stops due to infinitely many threads which does [XXX setDateString:].

After several useless tries I found this question/answer which tells me

do not use self. inside of custom accessors. access the variable directly,

so I made my code into

- (void)setDateString:(NSString *)newDateString {
    //self.dateString = newDateString;
    dateString = newDateString;
    NSInteger dateNumber = [dateString integerValue];

    // do something here..blah blah
}

then everything works like a charm!!


I am a junior developer of some objective languages, and a newbie to Objective-C.

I want to learn in details for this issue, instead of solving problems without understanding the reason.

So please provide me with some materials/website to understand more about this.

BTW, I use ARC.

Thank you all. :)

Community
  • 1
  • 1
tddian
  • 43
  • 8
  • 1
    Note that those aren't threads. Those are stack frames, which are quite different from threads. – Catfish_Man Jun 14 '12 at 19:04
  • You are right!! I was wondering why it does setter in several threads. Yes, they are stacks. – tddian Jun 14 '12 at 19:17
  • like [this](https://lh5.googleusercontent.com/-XI195OdmaBk/T9o5yyOrYDI/AAAAAAAAL00/o3EHym1nPWU/s800/Screen%2520Shot%25202012-06-15%2520at%25203.21.21%2520AM.png) – tddian Jun 14 '12 at 19:23

3 Answers3

3

The problem isn't actually this line:

NSInteger dateNumber = [dateString integerValue];

It is this line:

self.dateString = newDateString;

You see, that equals sign is akin to literally calling [self setDateString:newDateString] (in fact, that is what the compiler reinterprets it as) which creates an infinite loop. Naturally, commenting out that line would never have created that loop in the first place.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
3
self.dateString = newDateString;

is equivalent to

[self setDateString:newDateString];

so this will cause infinite recursion because you are calling it inside -setDateString:.

See also Difference between self.ivar and ivar?.

Community
  • 1
  • 1
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2

What happend is that you kept calling the function again and again causing a calling functions to fall in stak over flow

Whats wrong is int this line

self.dateString = newDateString;

This line is a objective c property, it has a getter and setters method, these method are automatically generated when you @synthesize it the setter and getters method for dateString are:

 - (void)setDateString:(NSString *)newDateString;

and

 - (NSString*)dateString;

So when you call self.dateString = newDateString; This line will call this function again

- (void)setDateString:(NSString *)newDateString {

And this function again contains the self.dateString = newDateString;, your call stack will get deeper and deeper till you are out of stack storage

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56