iPhone noob - setting NSMutableDictionary entry inside Singleton?
Posted
by codemonkey
on Stack Overflow
See other posts from Stack Overflow
or by codemonkey
Published on 2010-06-02T01:56:28Z
Indexed on
2010/06/02
2:04 UTC
Read the original article
Hit count: 345
Yet another iPhone/Objective-C noob question. 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() {
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. I'm probably doing something really obviously dumb? Any insights/suggestions will be appreciated.
© Stack Overflow or respective owner