0

I want to know which of these is considered "best practice" for iOS development:

  1. Declare all ivars and properties inside the .h file, so that anyone can understand the structure of the class.

  2. Declare all ivars within the @implementation block, in the .m file and declare necessary properties in .h file. (Also, if necessary declare some properties in .m file for internal use). The idea here being that others don't have to bother about my structure and implementation, they can just use my code.

jscs
  • 63,694
  • 13
  • 151
  • 195
Charith Nidarsha
  • 4,195
  • 3
  • 28
  • 32
  • See [this] (http://stackoverflow.com/questions/10330103/should-iboutlets-be-ivars-or-properties) and [this](http://stackoverflow.com/questions/3753130/should-i-use-properties-or-direct-reference-when-accessing-instance-variables-in) – msk Jul 31 '12 at 02:54

2 Answers2

1

It really is up to personal preference and differs on each project. Here are a couple examples:

When creating a framework, you will want to include only the properties and methods that you want others to see, as the .h file will be the only file they can really look into.

When working with a team on a large project, you may want to put more into the .h file so that when you track down a bug in a part of the code you didn't write, you know where to look to find the property.

I think most example online treat the header file as the "public" properties, and the .m as the "private" (even though they are not really private).

Anyway, as far as I have seen there is not a "best practice." Just make sure that you pick a method and stick to it throughout the project so that you have consistency.

Barlow Tucker
  • 6,229
  • 3
  • 36
  • 42
0

I think you had better read the developer document here it is:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17

cloosen
  • 993
  • 7
  • 17