DISCLAIMER: The fact that you are writing code in main.m suggests that you've start a console app project in Xcode and aren't trying to write a Cocoa or Cocoa Touch app.
If you are trying to write a Cocoa or Cocoa Touch application for OS X or iOS, your main.m file should look about like this:
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
And you should know what you're doing before you modify it (99.999% of the time, you'll never need to touch this file for a Cocoa/Cocoa Touch app).
So to restate, this answer assumes you're writing a simply console app to teach your self some Objective-C basics.
And if this assumption is incorrect, your question should be closed as unclear what you're asking.
Answer
Objective-C is a strict super-set of C. As such, anything we can do in C, we can also do in Objective-C. This includes declaring variables at the global scope. This is different from Java. In Java, everything is an object--everything resides in a class file. This includes the main method. Example:
public class MyApplication
{
public static Object[] myArray;
public static void main(String [] args)
{
// execution begins here
}
}
This is different from Objective-C. Not everything must reside in a class in Objective-C. If we want a global variable in Objective-C, we just declare it in the global scope. Example:
main.m
#import <Foundation/Foundation.h>
NSMutableArray * myArray;
int main(int argc, const char * argv[]) {
@autoreleasepool {
myArray = [NSMutableArray array];
}
return 0;
}
We can use myArray anywhere within main.m when we've declared it as such.
Let's note something here though... if it's not a member of a class, it's called a function, not a method. So we can declare C-style functions also in this global scope.
NSMutableArray * myArray;
void appendHelloWorld() {
[myArray addObject:@"Hello World"];
}
NSInteger myArrayCount() {
return myArray.count;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
myArray = [NSMutableArray array];
while (myArrayCount() < 10) {
appendHelloWorld();
}
NSLog(@"%@", myArray);
}
return 0;
}
This all works perfectly fine. If we run this program, we'll get the following output:
2015-05-01 13:22:21.188 ObjC[18340:4210463] (
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World"
)
But... this isn't the right way...
I've merely demonstrated to you exactly how to do exactly what your asking for, but we really don't want to get into doing things this way. This is the path to using globals as the answer to everything. This is the path to using singletons as the answer to everything. This is the path to writing applications that have 12 different singleton classes (I've seen it). This isn't the path to Sparta--this is the path to madness.
Instead, we should be dealing with instances of objects.
Our functions (or when we graduate to classes, our methods) should be passed an argument, and we shouldn't have a variable in the global scope.
So, let's get rid of the global variable and amend our methods:
void appendHelloWorld(NSMutableArray *array) {
[array addObject:@"Hello World"];
}
NSInteger arrayCount(NSArray *array) {
return array.count;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *myArray = [NSMutableArray array];
while (arrayCount(myArray) < 10) {
appendHelloWorld(myArray);
}
NSLog(@"%@", myArray);
}
return 0;
}
Running this program gives us identical output:
2015-05-01 13:27:48.594 ObjC[18361:4213356] (
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World",
"Hello World"
)
It also makes our code more flexible. Our function, appendHelloWorld isn't so tightly coupled to this code-based. It can be plunked out of this and into anywhere else and perform exactly as expected. It's not coupled to anything. It will drop the string, @"Hello World", onto any mutable array it is passed. And while arrayCount() is really unnecessary (just using it as an example), we can say all of the same things for it.
We don't want to use global variables and we don't want functions or methods so tightly coupled to global variables. We need to get comfortable with the idea of instantiating instances of objects and passing these objects to the methods or functions that need them.
Singletons should be used not when it is important for multiple classes to access some shared memory store--NO--that's not what singletons are for. Singletons should be used when you need to be certain only one instance of the class is instantiated ever in the life time of the app.