开发者

Setting NSMutableDictionary entry inside Singleton?

开发者 https://www.devze.com 2023-01-02 03:06 出处:网络
I\'m using a singleton to store app state information.I\'m including the singleton in a Utilities class that holds it (and eventually other stuff).This utilities class is in turn included and used fro

I'm using a singleton to store app state information. I'm including the singleton in a Utilities class that holds it (and eventually other stuff). This utilities class is in turn included and used from various view controllers, etc. The utilities class is set up like this:

// Utilities.h
#import <Foundation/Foundation.h>

@interface Utilities : NSObject {

}

+ (id)GetAppState;
- (id)GetAppDelegate;

@end

// Utilities.m
#import "Utilities.h"
#import "CHAPPAppDelegate.h"
#import "AppState.h"

@implementation Utilities

CHAPPAppDelegate* GetAppDelegate(开发者_Python百科) {
    return (CHAPPAppDelegate *)[UIApplication sharedApplication].delegate;
}

AppState* GetAppState() {
    return [GetAppDelegate() appState];
}

@end

... and the AppState singleton looks like this:

// AppState.h
#import <Foundation/Foundation.h>
@interface AppState : NSObject {
    NSMutableDictionary *challenge;
    NSString  *challengeID;
}
@property (nonatomic, retain) NSMutableDictionary *challenge;
@property (nonatomic, retain) NSString *challengeID;
+ (id)appState;
@end

// AppState.m
#import "AppState.h"

static AppState *neoAppState = nil;

@implementation AppState
@synthesize challengeID;
@synthesize challenge;

# pragma mark Singleton methods
+ (id)appState {
    @synchronized(self) {
        if (neoAppState == nil)
            [[self alloc] init];
    }
    return neoAppState;
}

+ (id)allocWithZone:(NSZone *)zone {
    @synchronized(self) {
        if (neoAppState == nil) {
            neoAppState = [super allocWithZone:zone];
            return neoAppState;
        }
    }
    return nil;
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (unsigned)retainCount {
    return UINT_MAX; //denotes an object that cannot be released
}

- (void)release {
    // never release
}

- (id)init {
    if (self = [super init]) {
        challengeID = [[NSString alloc] initWithString:@"0"];
        challenge = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)dealloc {
    // should never be called, but just here for clarity
    [super dealloc];
}
@end

... then, from a view controller I'm able to set the singleton's "challengeID" property like this:

[GetAppState() setValue:@"wassup" forKey:@"challengeID"];

... but when I try to set one of the "challenge" dictionary entry values like this:

[[GetAppState() challenge] setObject:@"wassup" forKey:@"wassup"];

... it fails giving me an "unrecognized selector sent..." error. Any insights/suggestions will be appreciated.


In your -init method, you should assign a retained dictionary to the ivar:

challenge = [[NSMutableDictionary alloc] init];
0

精彩评论

暂无评论...
验证码 换一张
取 消