Cocos2D 프레임 워크를 사용하여 Objective-C를 코딩 중이며 여러 목적으로 싱글 톤을 사용하고 있습니다. 하나의 새로운 목적은 문자열 인 캐릭터의 "상태"를 가져오고 설정하는 것입니다. 최근에이 목적을 위해 NSDictionary를 만들었지 만 싱글 톤 내부의 메서드가 호출 될 때 프로그램이 멈추는 문제가 있습니다.NSMutableDictionary 싱글 톤 문제
여기에 싱글 톤 코드가 있습니다. 난 그냥 문자 상태의 물건에 떠날거야 :
.H
@interface ExGlobal : NSObject {
NSArray *charStates_keys;
NSArray *charStates_objects;
NSMutableDictionary *charStates;
}
@property(nonatomic, retain) NSMutableDictionary *charStates;
+(ExGlobal*)sharedSingleton;
- (NSString *)charState:(NSString *)charName;
- (void)set_charState:(NSString *)value forCharName:(NSString *)charName;
@end
하는 .m
#import "ExGlobal.h"
@implementation ExGlobal
@synthesize charStates;
static ExGlobal* _sharedSingleton = nil;
+(ExGlobal*)sharedSingleton {
@synchronized([ExGlobal class]) {
if (!_sharedSingleton) {
[[self alloc] init];
}
return _sharedSingleton;
}
return nil;
}
+(id)alloc {
@synchronized([ExGlobal class]) {
NSAssert(_sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedSingleton = [super alloc];
return _sharedSingleton;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
// initialize stuff here
exitName = @"ruinsSkyMid";
sceneChangeKind = @"reborn";
charStates = [[NSMutableDictionary alloc] init];
charStates_keys = [NSArray arrayWithObjects:@"Feathers", @"Hummus", nil];
charStates_objects = [NSArray arrayWithObjects:@"at wall", @"with Feathers", nil];
charStates = [NSMutableDictionary dictionaryWithObjects:charStates_objects forKeys:charStates_keys];
}
return self;
}
- (NSString *)charState:(NSString *)charName{
NSString *value = [charStates objectForKey:charName];
return value;
}
- (void)set_charState:(NSString *)charState forCharName:(NSString *)charName{
[charStates setObject:charState forKey:charName];
}
- (void)dealloc {
//I know it doesn't get called, but just in case
[charStates release];
[super dealloc];
}
@end
그것은이 정지 할 때 정확히 문제가 무엇인지 나에게 불분명하다. 이 경우 콘솔에있는 모든 메시지는 다음과 같습니다.
Program received signal: “EXC_BAD_ACCESS”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
Previous frame inner to this frame (gdb could not unwind past this frame)
Previous frame inner to this frame (gdb could not unwind past this frame)
나는이 문제를 찾는 데 도움이되지 않습니다. charState_keys, charStates_objects 및 charStates를 charState 및 set_charState 메서드 내부에서 재정의하면 set_charState가 상태를 변경하지 않는다는 점을 제외하고는 작동하지 않는 것처럼 보입니다.
프로그래밍 문법에 감사드립니다.그러나 나는 그것을 어떻게 지키지 않습니까? – VagueExplanation
또한 시도했습니다 charStates = [[NSMutableDictionary alloc] retain]; 여기서 혼란스러워하는 종류. – VagueExplanation
또한 제 이해에서 dictionaryWithObjects는 자동 완성 사전이기 때문에 처음부터 유지할 필요조차 없습니다. 맞습니까? – VagueExplanation