morgancodes
morgancodes

Reputation: 25265

Objective-C: Is it ever Kosher to declare instance variables in the implementation file?

I've noticed that I'm able to declare instance variables in the implementation file of an Objective-C class. I understand there's a reason why I've been taught not to do this. Explained here. But it's so much nicer to not put them in the header, and my code seems to be running fine. How is this going to bite me in the butt down the road?

Upvotes: 1

Views: 510

Answers (1)

Joshua Weinberg
Joshua Weinberg

Reputation: 28688

There are two ways of declaring them in the implementation file.

Through a class continuation, which means placing another @interface block at the top of the file with an empty category name (they only JUST added the ability to add ivars here)

Or placing them just at the top of the file. When doing it like this, you are NOT adding ivars. This is the same as adding a variable to the top of a C file. They are in scope of the entire file, and every instance of the class that is in that file. These are global.

Upvotes: 4

Related Questions