views:

78

answers:

3

Hi

I have a singleton which I hold a single integer in, my score. However when I load it form the singleton in a another view it doesn't show what I want but instead sticks at a wired 6592960. Any ideas why? Something to do with how I am using @property Im thinking, what the correct way to @property an int?

Thanks.

EDIT: Code: Singleton .h

#import <Foundation/Foundation.h>


@interface scoreData : NSObject {

    int score;
}

@property int score;

+ (scoreData *)sharedData;

@end

Singleton .m

#import "scoreData.h"


static scoreData *sharedData = nil;

@implementation scoreData

@synthesize score;

#pragma mark -
#pragma mark Singleton Methods
+ (scoreData *)sharedData {
    if(sharedData == nil){
        sharedData = [[super allocWithZone:NULL] init];
    }
    return sharedData;
}
+ (id)allocWithZone:(NSZone *)zone {
    return [[self sharedManager] retain];
}
- (id)copyWithZone:(NSZone *)zone {
    return self;
}
- (id)retain {
    return self;
}
- (unsigned)retainCount {
    return NSUIntegerMax;
}
- (void)release {
    //do nothing
}
- (id)autorelease {
    return self;
}

@end

Inside my other view, I access like this and then try and change the number and of course I import the singleton:

scoreData *score = [scoreData sharedData];
score.score = 0;
A: 

If it's a true singleton, then you can have your getter and setter simply directly access an initialized static global variable, and skip all the runtime alloc and zone busywork.

hotpaw2
Could you elaborate please? Im fairly new to singletons.
Josh Kahane
If you don't need a to message a singleton, then for intrinsic data types you can just directly use a C global variable.
hotpaw2
A: 

I would write the @property like this:

@property (nonatomic,assign) int score;

Also i would change sharedData:

+ (scoreData *)sharedData {
    if( sharedData == nil ){
        sharedData = [[self alloc] init];
    }
    return sharedData;
}

Edit: I tried your code and got EXC_BADACCESS because it went recursive here:

+ (id)allocWithZone:(NSZone *)zone {
    return [[self sharedManager] retain];
}

When i removed that method everything worked as expected.

aegzorz
No such luck, still a weird of long number similar to originally posted.
Josh Kahane
Maybe you can use NSNumber instead, but you really shouldn't have to. I can't understand why that doesn't work, weird!
aegzorz
Josh how are you printing the number out? Perhaps your print or NSLog formatting is incorrectly showing you the wrong result...
twerdster
A: 

MySingleton.h

#import <Foundation/Foundation.h>

@interface MySingleton : NSObject {

}
@property(nonatomic,assign) int score;
+ (MySingleton*) sharedInstance;

@end

MySingleton.m

#import "MySingleton.h"

static MySingleton *_instance;
@implementation MySingleton
@synthesize score;

+ (MySingleton*)sharedInstance
{
    @synchronized(self) {
        if (_instance == nil) {
            _instance = [[super allocWithZone:NULL] init];
        }
    }
    return _instance;
}

#pragma mark Singleton Methods
+ (id)allocWithZone:(NSZone *)zone
{   
    return [[self sharedInstance]retain];   
}

- (id)copyWithZone:(NSZone *)zone
{
    return self;    
}

- (id)retain
{   
    return self;    
}

- (unsigned)retainCount
{
    return NSUIntegerMax; 
}

- (void)release
{
}

- (id)autorelease
{
    return self;    
}

@end

edit
See this xcode-template to simplify singleton-generation.

vikingosegundo