0

I want to create a variable that all other methods can access but it needs to be initialized in a specific method first. Help?

Specifically I'm creating an NSNotification that carries this variable that I need to make global. But I don't know what the variable's value is until it is received by the method via NSNotification.

- (void)getWallMapViewValue:(NSNotification *)notification
{

nMapView = [notification object];


}

nMapView is of type MKMapView and is declared but not set a value in the .h file so it is accessible to all other methods but is null to all other methods as well.

@public MKMapView *nMapView;

Not sure if this even can be done with a global variable so please tell me how I would go about doing it differently

1 Answer 1

0

You can initialize global variables with literals or nil. If you do not so, they are implicitly initialized with nil.

If you do want to initialize them with another value, you have to do that in a piece of code that is executed at the very beginning, for example in an +initialize method.

BTW: Global variables are a code smell. What do you want to do in detail?

1
  • I have two view controllers. 1.) map view controller 2.) table view controller. I want to send a notification when the map view controller loads to the table view controller. The notification has the MKMapView object I need for the table view controller method. I don't know what the MKMapView object is until the notification is received in the table view controller. Do you want more code?
    – stardev
    Dec 29, 2014 at 9:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.