Take the 2-minute tour ×

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 initialize 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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
add comment

Your Answer


