---
title: "Issue with global variables in Objective-C"  
description: "Issue with global variables in Objective-C"  
author: "zack mathews"  
published: 2015-12-15  
updated: 2015-12-15  
canonical: https://www.mindstick.com/forum/33733/issue-with-global-variables-in-objective-c  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 2 minutes  

---

# Issue with global variables in Objective-C

Learn this paragraph that is taken from the Cocoa [programming](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) [documentation](https://answers.mindstick.com/qa/30462/what-is-documentation):

```
You will be registering, reading, and setting defaults in several classes in your application. To make sure that you always use the same name, you should declare those strings in a single file and then simply #import that file into any file in which you use the names. There are several ways to do this. For example, you could use the C preprocessor’s #define command, but most Cocoa programmers use global variables for this purpose.
```

Is this really the correct best [practice](https://yourviews.mindstick.com/story/1498/terrific-benefits-of-a-morning-meditation-practice)? [Global](https://www.mindstick.com/articles/13127/macro-and-microeconomics-concepts-in-relation-to-global-organization-management) [variables](https://www.mindstick.com/articles/715/php-variables)? That seems insane to me – [counter](https://www.mindstick.com/interview/33859/how-to-create-pixel-counter-in-javascipt) to everything I’[ve ever](https://answers.mindstick.com/qa/36753/what-is-the-best-facebook-twitter-post-that-you-ve-ever-seen) been taught.

Would a better design be a simple [Singleton class](https://www.mindstick.com/interview/2473/what-is-singleton-class) with these defined? Or is it really the correct best practice to go global? Is there a better [pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-pattern) than either, given that many people consider Singletons to be globals in a pretty dress?

## Replies

### Reply by Tarun Kumar

According to the cocoa documentation I understand that global variables or a [singleton](https://www.mindstick.com/blog/10912/java-singleton-pattern) classes will accomplish the same thing here because both can be used to turn **'key'** names in Cocoa that would not throw a compiler error if it's misspelled into a compiler error. That's the main purpose. Global variables are a bit easier though seeing as it requires less typing.\
Instead of doing this:

```
[arrayObj setObject:newObj forKey:MyGlobalVariableKeyName];
```

we can do something along the lines of:

```
[arrayObj setObject:newObj forKey:[[MySingletonVariableClass getInstance] myVariableKeyName];
```

Global variables are essentially less typing for the same effect.


---

Original Source: https://www.mindstick.com/forum/33733/issue-with-global-variables-in-objective-c

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
