5

Hi I've seen two ways to declare hidden instance variables in the .m file 1)

@interface MyClass() 
{
  //my ivars
}
@end
@implementation MyClass 
 //my implementation
@end

2)

@implementation MyClass
{
 //my ivars
}
 //my implementation
@end

Confused as to the difference between the two, and/or coding conventions when it comes to this. Thanks for any help!

user2687305
  • 89
  • 1
  • 6
  • You will find your answers in http://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c The two options are valid, it just depends with Xcode version (read compiler) you are using. Better to mark this as a duplicate. – Luc Wollants Aug 15 '13 at 20:37
  • @LucWollants He is asking about instance variables and the linked question is referring to private methods. While they are similar I think that this is a not a duplicate of the above. – utahwithak Aug 15 '13 at 20:45
  • Should have been http://stackoverflow.com/questions/12632285/declaration-definition-of-variables-locations-in-objectivec – Luc Wollants Aug 15 '13 at 20:51

1 Answers1

0

Good question! You tend to see instance variables declared in the interface, but obviously the implementation works too. My thoughts on the issue:

  1. I think using the interface is better practice
  2. I think that ivars declared in the implementation are inaccessible to subclasses
Cole
  • 93
  • 7