Will Smith
Will Smith

Reputation: 349

How create global object in iPhone?

I am have one class named country , it has three NSMutableArray country_name, latitude and longitude. i need the object of country class in whole application. So how to declare country class object as global object?

Upvotes: 0

Views: 186

Answers (3)

Pfitz
Pfitz

Reputation: 7344

You can use the singleton design pattern and make a global object. But in general global variables / objects / singletons are a hint that something with your design is wrong.

Create a singleton this way:

+ (CardPainter*) sharedPainter { 
    static CardPainter* sp; 
    static dispatch_once_t token; 
    dispatch_once(&token, ^{  
        sp = [[CardPainter alloc] init]; 
    });
    return sp; }

Upvotes: 1

darko_5
darko_5

Reputation: 448

I recommend singleton like solution. Here is apple documentation with some explanation.

http://alexefish.com/post/15968663296/understanding-and-creating-singletons-in-objective-c

Upvotes: 0

Mutawe
Mutawe

Reputation: 6524

You can see this tutorial:

Creating a global object in objective-C

Upvotes: 0

Related Questions