1

In my code, I have about five @property statements; when I go to @synthesize = ... them, some give me choices (of the property name) in the dropdown menu of objects, and some do not. I also find that if I don't get all of the properties changed to _property in my methods, it makes no difference in execution; the app still works.

Why does the app still run whether or not the property name has an underscore as a prefix?

hypercrypt
  • 15,389
  • 6
  • 48
  • 59
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • 2
    No idea about your first question but the underscore thing is to protect against typing `ivar = ...` when you really want `self.ivar = ...`. The first of those ignores the attributes you declare for the property and leads to some common bugs. (Of course, you can always type `_ivar = ...` but it's less likely to be an accident.) – Phillip Mills Jun 29 '12 at 22:10

2 Answers2

2

You should use @synthesize iVar = _iVar;, but maybe soon you won't need to synthesize at all ;)

I would say that the options given by Xcode are just the code completion being a bit buggy.

Strictly speaking, the name you use doesn't matter at all (as long as there aren't any collisions), @synthesize iVar = i_va_R; is perfectly legal, so is:

@synthesize foo = bar;
@synthesize bar = foo;
// DO NOT DO THIS IN REAL LIFE!!

All you are saying is that you want the property to be backed by an instance variable with the given name. If you never access the instance variable directly (always use self.iVar rather than iVar or _iVar) then it doesn't really matter at all what you use, it only matters to the rest of the code if you want to access the iVar directly.

Just to clarify a little more, here is an example of of how the above could be implemented without using properties:

@interface MyBadClass {
    int foo;
    int bar;
}

- (int)foo;
- (void)setFoo:(int)foo;

- (int)bar;
- (void)setBar:(int)bar;

@end

@implementation MyBadClass

- (it)foo
{
    return bar;
}

- (void)setFoo:(int)foo
{
    bar = foo;
}

- (int)bar
{
    return foo;
}

- (void)setBar:(int)bar
{
    foo = bar;
}

@end

(PS: I am using ints to avoid dealing with memory management, the same applies to objects too though)

hypercrypt
  • 15,389
  • 6
  • 48
  • 59
  • Why does the app still run if I don't have all of the occurrences changed or not? – SpokaneDude Jun 29 '12 at 22:47
  • I've seen the (at)synthesize iVar = _iVar; syntax before, but never quite understood it until I hit this question. Assuming I'm reading between the lines above and here, are you saying that (at)synthesize foo = bar means that while the actual iVar name might be 'bar' (in the (at)property block, for example), but the setter/getter functions use 'foo' (setFoo, getFoo) instead? (Appologies for using (at) instead of the appropriate symbol, but that is apparently a reserved character in comments ) – RonLugge Jun 29 '12 at 22:52
  • hypercrypt: so are you saying that forget the @synthesize Foo = _Foo, and instead use 'self.Foo' wherever you want to access Foo? – SpokaneDude Jun 29 '12 at 22:59
  • If you still need to add `@synthesize` manually then you should add `@synthesize foo = _foo;`, but use `self.foo` instead of `_foo` everywhere except in `-init...`, `-dealloc`, `-foo` and `-setFoo:` – hypercrypt Jun 29 '12 at 23:02
  • hypercrypt: one more Q: you mentioned in your first comment that 'but maybe soon you won't need to synthesize at all ;)'.. what did you imply by that? :D – SpokaneDude Jun 29 '12 at 23:05
  • @spokane-dude See [this question](http://stackoverflow.com/q/9347722/643383). Upcoming versions of the compiler will synthesize your ivars automatically -- no need to add the `@synthesize` directive. – Caleb Jun 29 '12 at 23:07
0

In 32bit runtime you may have problems as properties backing ivars aren't created automatically... In 64 bit mode or iPhone if you make an IVar called _var and a property var, then dont specify _var when synthisizing you will have 2 vars, but it will be broken in 32 bit Mac OS

Grady Player
  • 14,399
  • 2
  • 48
  • 76