Singleton Class无法设置属性

时间:2013-11-20 05:50:17

标签: objective-c memory-management singleton

main.m
  ... 
-(id) init{
     [[Singleton sharedSingleton] set_player:[CCSprite spriteWithFile:@"player.png"]]; 
...

每次都会引发错误的访问错误。我该如何解决?

Singleton.h

#import <Foundation/Foundation.h>
#imzort "cocos2d.h"

@interface Singleton : NSObject {
    NSMutableArray *_enemies;
    NSMutableArray *_projectiles;
    NSMutableArray *_enemyProjectiles;
    NSMutableArray *_enemyProjectilesToDelete;
    NSMutableArray *_projectilesToDelete;
    CGSize _winSize;
    CCSprite *_player;
}
@property (nonatomic, strong) NSMutableArray *_enemies;
@property (nonatomic, strong) NSMutableArray *_projectiles;
@property (nonatomic, strong) NSMutableArray *_enemyProjectiles;
@property (nonatomic, strong) NSMutableArray *_enemyProjectilesToDelete ;
@property (nonatomic, strong) NSMutableArray *_projectilesToDelete;

@property (nonatomic, strong) CCSprite *_player;
@property (nonatomic, assign) CGSize _winSize;
+(Singleton *)sharedSingleton; 
@end

Singleton.m

#import "Singleton.h"


@implementation Singleton
@synthesize _enemies,_projectiles,_enemyProjectiles,_enemyProjectilesToDelete,_projectilesToDelete,_winSize, _player;

+(Singleton *)sharedSingleton{
    static Singleton *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedSingleton = [[self alloc] init];
    });
    return _sharedSingleton;
}
-(id)init{
    if(self = [super init]){
        _enemies = [[NSMutableArray alloc]init];
        _projectiles = [[NSMutableArray alloc]init];
        _projectilesToDelete = [[NSMutableArray alloc]init];
        _enemyProjectiles = [[NSMutableArray alloc]init];
        _enemyProjectilesToDelete = [[NSMutableArray alloc]init];


        _winSize = [CCDirector sharedDirector].winSize;
        _player = [CCSprite spriteWithFile:@"player.png"];
    }
    return self;
}

@end

我找不到错误代码,但在CCNode.m中,xcode中断:

[_children makeObjectesPerformSelector:@selector(onEnter)];

出现EXC_BAD_ACCESS错误。

编辑: 更改     @property(非原子,强)CCSprite * _player;

要     @property(nonatomic,assign)CCSprite * _player;

解决了这个问题,但我不知道该解决方案是否真的是一个'单例'解决方案。

0 个答案:

没有答案
相关问题