(Objective C) what is the advantage of doing @synthesize myvar = _myvar (if any)? [duplicate]

I’ve wondered this many times myself. Interested in other people’s answer, but one reason I’ve found is that it forces you to notice if you’re accessing the ivar directly when you should be using the getter/setter.

self.myvar = @"blah"; and _myvar = @"blah";

vs

self.myvar = @"blah"; and myvar = @"blah";

It’s easy to leave the self. out by accident… it’s a lot harder to put the _ in by accident.

Leave a Comment