English 中文(简体)
页: 1 单一语文问题
原标题:NSMutableDictionary Singleton issue

我正在利用可可2D框架来编码目标C,我有一个用于多重目的的单一吨。 一个新目的就是获取和确定描述的“状态”。 我最近为此做了一个NSDictionary,但我对冻结方案有问题,因为采用单一州的方法。

这里有单州法典。 仅剩下一只字体:

<>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 

<>

#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)

我肯定不会帮助找到这一问题。 我发现,如果我重新界定了沙尔邦内部的key子、char子和char子,并制定了“查尔邦”方法,那么,除了“查尔邦”没有改变国家的情况外,它似乎没有冻结。

最佳回答

这是一种冻结,正在坠毁。 因此,<代码>EXC_BAD_ACCESS。 它认为像你的X条码安装一样,因为以下两条电文不应出现。

请注意,方法不应有_s in the name;不是问题的起因,而是关于以下公约的评论。

页: 1

问题回答

Not an answer as such but I didn t have enough space in the comments field above to post this, but it might be useful.

正如泡沫已经说过的那样,你没有保留国可能是问题。

如果你在保留和保留物体时混淆不清,则有一本名为“在澳门学习目标”的书,我知道这本书是一本Macbook,但其中多数也适用于i。 第9章(渔业管理)第171页谈到“综合管理规则”,以及如果你被混淆到何时保留还是不保留,那么你就不理解客观记忆管理的简单规则。

Essentially if you create an object using new, alloc or copy, then the retain count is automatically set to 1 so the object is retained and does not require you to retain it and will require a subsequent release to deallocate.

如果你以任何其他方式制造该物体,该物体将成为自动释放的物体。

显然,这些规则只适用于标准图书馆,而且不一定适用于第三方图书馆。

我建议在目标C中完全理解记忆管理的人读本书。 我甚至认为我的工作很有启发性。

希望。





相关问题
PHP object Singleton not working

I am programming a website backend in PHP and I need to encapsulate $_SESSION in a class. I ve made my Session class a singleton but I am having trouble using it. class Session { private static $...

Singleton pattern + __construct in PHP4

To clarify: no I can t make this pure PHP5 yes this code works in PHP 4.3.9. I don t have any real world experience implementing or supporting PHP4 but I had to recently change my class so it ...

ASP.NET Webforms IHttpModule Singleton

I have a class that implements IHttpModule in a separate assembly from a website. The module implementation intercepts requests and rewrites urls for the website. The mappings are stored in a ...

Should WCF service typically be singleton or not?

I believe Jimmy Nillson said he generally made his webservices singletons. Is this the preferred method, and with WCF? Other than making the service methods static, is there something else to be done?

Java: unable to access static singleton method

i am facing one problem.i have a class named "ReportingService" and it should be singleton and is is extending "CommonService". package MyApp.Services.ReportingService; public class ReportingService ...

C++ Threadsafe Singleton (NOT FOR INIT)

So I want to access a singleton class from multiple threads. Conceptually I d think that calling non-const methods on this singleton instance would be not thread-safe. I ve been looking online and no ...

@staticmethod with @property

I want Stats.singleton.twitter_count += 1 and I thought I could do class Stats: singleton_object = None @property @staticmethod def singleton(): if Stats.singleton_object: ...

热门标签