1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.