开发者

Is there any problem with the usage of static variables in Objective-C?

开发者 https://www.devze.com 2023-03-07 15:20 出处:网络
Is there any problem with the usage of static variable in Objective-C? If yes, please explain. Reason for usage of static variable

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

  1. MainView
  2. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消