Global variables in Objective-C on iPhone

I am new to Objective-C with a background mainly in database programming. I am developing a medical application for the iPhone, which includes several formulas for calculations using many variables. In fact, each formula will have its own screen, but the numerical entries and calculations from each screen should appear on the screens for other formulas that have common entries, as the user moves between the screens, and the values ​​of the variables changed on one screen must be updated. if they appear on another screen.

I understand the potential traps of global variables, but it seems like I need a set of global variables. I am wondering what is the best method to achieve this. I reviewed the use of singletones on this site and in the Apple documentation, and perhaps this method is a solution. I was also interested, perhaps, if you delve into Core Data, this is the right way, preserving the numerical variables entered or calculated on one screen, which will be obtained when a new formula appears on another screen.

Thanks in advance for any advice.

+1
source share
1 answer

The common architecture for what you ask is an exemplary part of the Model-View-Controller design pattern.

You must define your own set of calculations and the data that they use in your model. A model consists of one or more Objective-C classes containing values ​​from your calculations in instance variables. The methods in your model classes do the actual math. You do not need global variables if your model gives you access to all related variables. The only variable you need access to is the model object itself, which can be stored in the application delegate.

The model is just an abstract description, it does not contain parts of the user interface. You will use the controller class to bind your model to user interface elements, such as text fields or buttons.

See Apple MVC's description here or here .

+3
source

Source: https://habr.com/ru/post/920832/


All Articles