Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在Singleton中设置NSMutableDictionary条目?_Iphone_Objective C_Cocoa_Cocoa Touch_Singleton - Fatal编程技术网

Iphone 在Singleton中设置NSMutableDictionary条目?

Iphone 在Singleton中设置NSMutableDictionary条目?,iphone,objective-c,cocoa,cocoa-touch,singleton,Iphone,Objective C,Cocoa,Cocoa Touch,Singleton,我正在使用单例存储应用程序状态信息。我将singleton包含在一个实用程序类中,该类包含它(最终还有其他东西)。该实用程序类依次包含在各种视图控制器等中,并从中使用。实用程序类的设置如下: // Utilities.h #import <Foundation/Foundation.h> @interface Utilities : NSObject { } + (id)GetAppState; - (id)GetAppDelegate; @end // Utilities

我正在使用单例存储应用程序状态信息。我将singleton包含在一个实用程序类中,该类包含它(最终还有其他东西)。该实用程序类依次包含在各种视图控制器等中,并从中使用。实用程序类的设置如下:

// 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
// 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
[GetAppState() setValue:@"wassup" forKey:@"challengeID"];
[[GetAppState() challenge] setObject:@"wassup" forKey:@"wassup"];
。。。但当我尝试设置一个“挑战”字典条目值时,如下所示:

// 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
// 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
[GetAppState() setValue:@"wassup" forKey:@"challengeID"];
[[GetAppState() challenge] setObject:@"wassup" forKey:@"wassup"];

。。。它无法向我提供“已发送无法识别的选择器…”错误。如有任何见解/建议,我们将不胜感激。

在您的
-init
方法中,您应该为ivar分配一个保留词典:

challenge = [[NSMutableDictionary alloc] init];

-init
方法中,应该为ivar分配一个保留字典:

challenge = [[NSMutableDictionary alloc] init];

哇!这么小的事情。。。我的头重重地撞在墙上。非常感谢。在系统允许的情况下尽快将此标记为答案。哦。。。我还看了你的博客关于单身的警告。我相信我还是会用这个,但是我会考虑自己的警告。再次感谢。。。。在这种情况下,它不仅仅是第二双眼睛。。。你的眼睛其实明白他们在看什么。。。我的,在objective-c中仍然没有那么多。。。这么小的事情。。。我的头重重地撞在墙上。非常感谢。在系统允许的情况下尽快将此标记为答案。哦。。。我还看了你的博客关于单身的警告。我相信我还是会用这个,但是我会考虑自己的警告。再次感谢。。。。在这种情况下,它不仅仅是第二双眼睛。。。你的眼睛其实明白他们在看什么。。。我的,在objective-c中仍然没有那么多。仅供参考,您的实用程序类完全是空的。声明两个方法,但不实现它们,而是使用相同的名称编写函数。您可以完全删除Utilities类。仅供参考,您的Utilities类完全为空。声明两个方法,但不实现它们,而是使用相同的名称编写函数。您可以完全删除Utilities类。