My problem is that I can access methods and attributes from the sharedInstance Singleton in one class but I cannot in another.
For example, the code below works and is recognized by X-code. Works fine. return [[[SINGLETON sharedInstance]baseballArray]count];
In addition to:
theSelectedBaseball = [[[SINGLETON sharedInstance]baseballArray]objectAtIndex:indexPath.row];
SINGLETON *singleton = [SINGLETON sharedInstance];
[singleton setSelectedBaseball:theSelectedBaseball];
However, if I try the code above in another class I get this warning message: - Method - setSelectedBaseball: not found.
I am importing the SINGLETON header in all of the classes that I want to use it. I looked closely at the class that it is being recognized versus the other class that it is not and I can't figure out why it is not being recognized.
Here is my Singleton Class.
#import <Foundation/Foundation.h>
#import "Baseball.h"
@interface SINGLETON : NSObject {
NSArray *baseballArray;
Baseball *selectedBaseball;
}
@property (nonatomic, retain) NSArray *baseballArray;
@property (nonatomic, retain) Baseball *selectedBaseball;
+ (SINGLETON*) sharedInstance;
- (void)setSelectedBaseball:(Baseball *)theBaseball;
- (Baseball*)getSelectedBaseball;
@end
Implementation:
#import "SINGLETON.h"
#import "Baseball.h"
@implementation SINGLETON
@synthesize baseballArray, selectedBaseball;
static SINGLETON *instance = nil;
+ (SINGLETON*)sharedInstance
{
@synchronized(self) {
if (instance == nil) {
instance = [[SINGLETON alloc] init];
}
return instance;
}
}
- (void)setSelectedBaseball:(Baseball *)theBaseball
{
selectedBaseball = theBaseball;
}
- (Baseball*)getSelectedBaseball{
return selectedBaseball;
}
- (id)init
{
self = [super init];
if (self) {
// created 5 Baseball Objects
// baseball array holding those 5 baseball objects
baseballArray = [[[NSArray alloc] initWithObjects:Baseball1, Baseball2, Baseball3, Baseball4, Baseball5, nil] retain];
// dealloced 5 Baseball Objects
}
return self;
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (instance == nil) {
instance = [super allocWithZone:zone];
return instance; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}
- (id)retain
{
return self;
}
- (unsigned)retainCount
{
return UINT_MAX; //denotes an object that cannot be released
}
- (id)autorelease
{
return self;
}
- (void) dealloc
{
[baseballArray release];
[super dealloc];
}
@end