5

G'Day Programmers, I am from Java background however I have just started learning C++ and Objective C. I was worried when I so lots of different coding style in third party Objective C code. But I am kind a stuck with a dilemma.

My dilemma is whether to use #pragma tags while coding iOS application? Does it considered to be a good practise? Or it is programmer's own choice to have those directive drop down links?

Your expertise and industry experience will be helpful,

Thanks


  • Content I searched on internet were mostly suggesting what #pragma does. But I couldn't find much information regarding whether it is considered to be good practise or not.
TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • As for C++: *Why* do you think you need `#pragma`? Do without it unless you have a watertight explanation why you need it. – Kerrek SB Nov 09 '11 at 12:53
  • As I said I am naive, I will remove c++ tag :) – TeaCupApp Nov 09 '11 at 13:04
  • 1
    In C and C++ I would consider using a pragma as a NoNo in pretty much all cases. In ObjectiveC/C++ using a pragma for seperating is considered to be good and helpful. Note that ObjectiveC/C++ is commonly bound to be used on a single platform (MacOS / iOS). Pragmas usually are used for compiler specific instruction which is bad as it weakens portability. – Till Nov 09 '11 at 13:06
  • @Till: you don't need `#pragma` at all in Objective-C. See my comment to alexandre's answer. – JeremyP Nov 09 '11 at 13:46
  • @JeremyP well spotted. However, the Xcode by default, uses those pragmas within the templates, hence it "poisons" all projects with pragmas by default. This again drives me to give in and stick to use pragmas myself as well. Still, you are right and using the comment-variant for marks seems a lot prettier. – Till Nov 09 '11 at 15:01
  • 1
    @Till: yes, there is a lot about XCode 4 that seems wrong to me :) – JeremyP Nov 09 '11 at 15:07

2 Answers2

6

I usually use #pragma to separate implementations, like:

#pragma mark UITableViewDelegate Methods

#pragma mark Custom functionality Methods

So i can look at the drop down menu and go directly to where i want.

alexandresoli
  • 918
  • 1
  • 9
  • 18
  • Ohh ok so it all depends on us whether we need to set those drop down links or not? Thanks Man! – TeaCupApp Nov 09 '11 at 13:03
  • 5
    @Brogrammer: You don't need to use `#pragma` for that. A comment like `// MARK: some title` works just as well. Also, `// MARK: -` gives you a separator in the menu – JeremyP Nov 09 '11 at 13:43
  • And, after all, this is specific to **one single IDE: Xcode.** Even this becomes unhelpful if you don't use Xcode. –  Oct 28 '12 at 12:35
4

I also use

 #pragma mark - UISomeDelegate

or

 #pragma mark -

XCode separates methods list with a line in navigation bar:

Line above UISomeDelegate

Vadym
  • 1,067
  • 1
  • 13
  • 24