2

What's the difference between putting a variable inside of the @interface{} declaration in a header file, and putting in a @property after that declaration?

E.g.,

@interface GameCenterManager : NSObject
{
GKInvite* pendingInvite;
}
@end

as opposed to

@interface GameCenterManager : NSObject
@property (weak, nonatomic) GKInvite* pendingInvite
@end
jraede
  • 6,846
  • 5
  • 29
  • 32
  • Similar to this question here: http://stackoverflow.com/questions/4172810/what-is-the-difference-between-ivars-and-properties-in-objective-c – Valent Richie May 21 '13 at 04:09

1 Answers1

3

Declaring a property generates getters and setters for the instance variable, according to the criteria within the parenthesis.

Defining the variables in the brackets simply declares them instance variables.

Following are some links that provides more info on these.

http://www.cocoawithlove.com/2010/03/dynamic-ivars-solving-fragile-base.html

Is there a difference between an "instance variable" and a "property" in Objective-c?

http://iphonedevelopment.blogspot.in/2008/12/outlets-property-vs-instance-variable.html

Community
  • 1
  • 1
Vinayak Kini
  • 2,919
  • 21
  • 35
  • Not sure, but to generate the getter and setter you must synthesize no? – Vertig0 May 21 '13 at 04:06
  • 1
    @PatricioIgnacioFariaValdivi Not anymore. Just `@property` is enough, it'll create an iVar automatically with a underscore prefix. You can test this with `@property (nonatomic) NSString *foo` in interface, without synthesize you can get _foo in you implementation. – iNoob May 21 '13 at 04:09
  • 1
    @PatricioIgnacioFariaValdivi You need not synthesize from ios6 as it is synthesized automatically by the system. where as previous versions need you to synthesize manually, – Vinayak Kini May 21 '13 at 04:11
  • Awesome, good to know, thanks. But well, if he is using and iOS < 6 he must synthesize :) – Vertig0 May 21 '13 at 04:14
  • @PatricioIgnacioFariaValdivi yes exactly! :) – Vinayak Kini May 21 '13 at 04:16
  • It's not the operating system that matters, it's the compiler, @VinayakKini. – jscs May 21 '13 at 04:46
  • @JoshCaswell Yes it should be compiler. Thanks for helping me to rectify my answer :) – Vinayak Kini May 21 '13 at 04:52