Variable declaration difference in Objective-C -
I was reading a tutorial about core image in iOS 6. In that tutorial I found something like this:
@implementation ViewController {CIContext * context; CI Filter * Filter; CIMES * Start Image; UIImageOrientation Orientation; } // Some methods @end After the @implementation statement, the variable has been declared in .m file in a consolidation. I am looking at these types of variable declaration for the first time.
Is there any difference between the above variable declaration and the following code
@implementation ViewController CIContext * context; CI Filter * Filter; CIMES * Start Image; UIImageOrientation orientation; // some methods @end
There is a big difference. Variables inside parentheses directly after
@interface or @implementation example variable . These are the variables associated with each instance of your class, and thus are accessible anywhere in your instance methods. If you do not insert brackets, you declare the global variable . Any variable declared outside any bracket block will be a global variable, even before the variables are @implementation before or after the instruction. And global variables are wrong and at all costs (you can declare global stability, but you can leave global variables), especially because they are not thread-safe (and this By the way, these bugs can arise (messed up for debug).
In fact, in the historical (in the earlier versions of Objective-C and Compiler), you can only send your @interface < / Code> In your .h file // h @ interface your Plus: ParentClass {// Example frequency characters int ivar1;} // Here, declare the frequency and class methods, as well as properties (which is nothing more than Gayatri / Setter instance method) - (zero) printwars; End // I have some global variables; // Global Variable (Bad Idea !!) @implementation YourClass int is some other global variable; // Still a bad idea - (zero) printworld {nslog (@ "eyeware =% d" , Ivar1); / / You can use ivar1 because It is a frequency variable / every instance of your class (using [[YourClass alloc] init) will have its own value for ivar1} Only modern compilers will give you their own Inside the class extension ( @interface YourClass () . In the M implementation file) or in your @implementation , declare example variables (still in parentheses) in your .h in @interface After the possibility of declaring them, along with the advantages to hide those example variables of external users of those sections, the .m file and now no longer in the .h file, because your class user is concerned about the internal coding details of your class There is no need to be aware of Which is only necessary to know the public API.
One last advice: Instead of using the example variable, the apple recommends using directly more and more @property , and the compiler is clearly (The implications of the @synth escape directive, or the modern LLVM compilers) generate internal backing variables. So in the end you will not usually need to declare the frequency variable, and after {}> after @interface directive: pre> < Code> // H @interface yourClass: ParentClass // Promotion methods and properties @property (nonatomic, assign) int prop1; - (zero) printProp; @ And // M @ Implementation YourClass // @ synthesize prop1; // It is not even necessary with the modern LLVM compiler - (zero) PrintPop {NSLog (@ "Iver =% D", self.prop1); }
Comments
Post a Comment