2011-09-14 4 views
0

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가 상태를 변경하지 않는다는 점을 제외하고는 작동하지 않는 것처럼 보입니다.

답변

2

얼지 않고 충돌합니다. 따라서 EXC_BAD_ACCESS. 다음 두 메시지가 발생해서는 안되기 때문에 Xcode 설치가 borked 된 것 같습니다.

메소드에 이름에 _이 없어야합니다. 문제의 원인이 아니라 협약 준수에 대한 의견.

너는 charStates을 유지하지 못하고 그 원인 일 가능성이 높습니다.

+0

프로그래밍 문법에 감사드립니다.그러나 나는 그것을 어떻게 지키지 않습니까? – VagueExplanation

+0

또한 시도했습니다 charStates = [[NSMutableDictionary alloc] retain]; 여기서 혼란스러워하는 종류. – VagueExplanation

+0

또한 제 이해에서 dictionaryWithObjects는 자동 완성 사전이기 때문에 처음부터 유지할 필요조차 없습니다. 맞습니까? – VagueExplanation

2

답변이 없지만 위의 댓글 입력란에 충분한 공간이 없지만 유용 할 수 있습니다.

bbum이 이미 말했듯이 charStates를 유지하지 못하는 것이 문제 일 수 있습니다.

개체를 보관하지 않을 때 혼란 스러우면 "Mac에서 Objective-C 배우기"라는 정말 좋은 책이 있는데 그 책은 Mac 책이지만 대부분이 iPhone에도 적용됩니다. 9 장 (메모리 관리)의 171 페이지에서 "메모리 관리 규칙"에 대해 이야기하고 Objective C 메모리 관리의 간단한 규칙을 이해하지 못하거나 유지하지 않을 때 혼동하는 경우를 설명합니다.

기본적으로 new, alloc 또는 copy를 사용하여 객체를 만드는 경우 보유 수는 자동으로 1로 설정되므로 객체가 유지되고 객체를 보유 할 필요가 없으며 할당 해제를 위해 후속 릴리스가 필요합니다.

다른 방법으로 개체를 만드는 경우 해당 개체는 자동 렌더링 된 개체가됩니다.

분명히 이러한 규칙은 표준 iOS 라이브러리에만 적용되며 제 3 자 라이브러리에는 반드시 적용 할 수 없습니다.

Objective C의 메모리 관리를 완전히 이해하지 못하는 사람은이 책을 읽는 것이 좋습니다. 나는 심지어 내 iPhone 작업을 위해 매우 계몽적인 것을 발견했다.

희망 /.