Is there any problem with the usage of static variables in Objective-C?
Is there any problem with the usage of static variable in Objective-C? If yes, please explain.
Reason for usage of static variable
I have two classes
- MainView
- WifiConnection
When the application loads, I am in the MainView class and will call a method from the WifiConnection class(method name:send).send
method to initi开发者_Python百科alize instream and outstream which is declared in the WiFiConnection class.
So it will send data successfully the first time. But sending the second time I do not need to initialize instream and outstream which is already initialized.
But this time when I look instream and outstream value it will be null. So I declared these variables as static and solved the above problem.
And also, how do I declare a class type variable as global?
Since you need only a single instance of the class, you can take a look at Singletons. They are based of static variables. This will involve some minor modification to your code and you can call functions on instances like [[MainView sharedMainView] showMessage]
.
This is the best approach of using static variables.
精彩评论