views:

42

answers:

3

I want to make a class that can hold settings for my app. Its pretty basic. I will access it from a few different classes, but only want 1 version of this container class to exist so they all see the same data.

Is there something special i need to flag for this?

Here is what I've done so far: Filters.h

#import <Foundation/Foundation.h>
@interface Filters : NSObject 
{
    NSString    *fuelType;
}
@property (nonatomic, copy) NSString *fuelType;
@end

Filters.m

#import "Filters.h"
@implementation Filters
@synthesize fuelType;
@end

Is there some flag i need to use when i alloc an instance of this class or how should I work this if I need to access the fuelType value from 2 different classes?

Thanks -Code

+1  A: 

What you're looking for is a singleton. Most people advise against using singletons though as it is often considered "dirty". See "Singleton" in this apple doc to learn more about it.

Toastor
I don't know about people pooh-poohing singletons. I think they're a fundamental Cocoa design pattern.
Dan Ray
Well when I was first looking for help on using singletons, I've repeatedly read that it would tempt people to write dirty code, just like using global variables would. However that's an opinion I do not actually share, I just mentioned it because so far it appeared to me like the majority of people would agree with it. But I might be wrong.
Toastor
+1  A: 

You need a singleton: you can create your singleton by your own or use AppDelegate object which is an object that is always alive and never released while your application in the frontground (just put the vars needed there and initialize them dynamically).

Here are some links on how to create a singleton. Cocoa fundamental Guide: Creating a Singleton and CocoaDev Singleton Pattern

nacho4d
Wow I've read those. Gone way over my head :) I google some Singleton tuts. Thanks nacho4d
Code
+2  A: 

Hey Code

For global application settings a better way would be to use NSUserDefaults or if you want to store data for use you should look up using core data and sqlite.

Lastly, if you want to go for ease of use you could do a core data style app delegate class and grab it by using:

[[[UIApplication sharedApplication] delegate] myClass] that way you'll always have that version of the class.

Thomas Clayson