0

I know this can be done with many languages, but I don't see how to do it using Objective-C. I've read about singletons but as they are designed to be instanciated only once, they do not feed this need.

So this class could be called like this :

MyClass* obj1 = [[MyClass alloc] initWithKey:@"oneKey"];
NSString* lib = obj1.lib;

or

int id = [MyClass idForKey:@"anotherKey"];

I've tried this code but I'm pretty sure it's really bad, but I don't see how to achieve this :

.h file

@interface MyClass : NSObject {
    NSString* key;
}

@property(nonatomic, retain) NSString* key;
@property(nonatomic, readonly) int id;
@property(nonatomic, readonly) NSString* lib;
@property(nonatomic, readonly) int value;

+ (id) classWithKey:(NSString*)theKey;

@end

.m file

#import "MyClass.h"

@interface MyClass.h (Private)
-(id)initWithKey:(NSString*)theKey;
@end

@implementation MyClass

@synthesize key;

static NSMutableDictionary* vars = nil;


-(id)init 
{
    if (!(self = [super init])) return nil;
    self.key = nil;
    [MyClass initVars];
    return self;
}


-(id)initWithKey:(NSString*)theKey 
{
    if (!(self = [super init])) return nil;
    self.key = theKey;
    [MyClass initVars];
    return self;
}


+ (id) classWithKey:(NSString*) theKey 
{
    return [[[MyClass alloc] initWithKey:theKey] autorelease];
}


+(void)initVars
{
    if (vars != nil) return;

#define mNum(x) [NSNumber numberWithInt:x]
#define k0 @"id"
#define k1 @"lib"
#define k2 @"val"

    vars = [NSMutableDictionary dictionary];
    [vars setObject:[NSDictionary dictionaryWithObjectsAndKeys:mNum(5), k0,  @"One value", k1,       mNum(0), k2, nil] forKey:@"oneKey"];
    [vars setObject:[NSDictionary dictionaryWithObjectsAndKeys:mNum(8), k0,  @"Another value", k1,   mNum(1), k2, nil] forKey:@"anotherKey"];
    ...

    [vars retain];
}


- (int)id          { return [[[vars objectForKey:self.key] objectForKey:k0] intValue]; }
- (NSString*)lib   { return [[vars objectForKey:self.key] objectForKey:k1]; }
- (int)value       { return [[[vars objectForKey:self.key] objectForKey:k2] intValue]; }


-(void)dealloc 
{
    self.key = nil;
    [vars release];
    [super dealloc];
}

+(int) idForKey:(NSString*)theKey
{
    if (vars == nil) [self initVars];
    return [[[vars objectForKey: theKey] objectForKey:k0] intValue];
}

@end
Oliver
  • 23,072
  • 33
  • 138
  • 230
  • singleton are designed to be instantiated JUST ONCE, then all other call to instantiate that class just don't instantiate a new instance, but just get back the first instantiation of it – meronix Mar 04 '12 at 17:42
  • @meronix:That's why I think singleton won't help. I need a classs that can be instanciated, and that has a shared var that can also be accessed by class method. – Oliver Mar 04 '12 at 20:46
  • ok, but then why not to move JUST that shared var, used by your class in all its instances, in a new singleton class? – meronix Mar 04 '12 at 21:01
  • ...but i may have misunderstood your question: has that array the same value (=pointing to the same array) in all the instances of your class (that's a good definition of a singleton..), or not? – meronix Mar 04 '12 at 21:16
  • @meronix:Yes. The goal is to have a class that has some definitions in an array, and being able to instanciate that class to represent one of these values. Without having to make classes and classes for this simple thing. I remember to have done that in c++ years ago. A class, that can be instanciated, and that shares some same properties with all instances of that class. – Oliver Mar 04 '12 at 23:09
  • Can you give a concrete example of what you are trying to achieve? This sounds like something of an anti-pattern. – Conrad Shultz Mar 05 '12 at 08:35
  • @ConradShultz: you can see my edit – Oliver Mar 05 '12 at 09:40
  • I read your code, but what I want to know is the bigger picture: how is this class being used? What problem are you trying to solve? Describe the context within your application. – Conrad Shultz Mar 05 '12 at 16:48

1 Answers1

-1

take a look at singleton class concept there are a lot of answer for singletons, just search

here's' one:

Is this really a singleton?

Community
  • 1
  • 1
meronix
  • 6,175
  • 1
  • 23
  • 36