Objective-C Singleton problem. Object recognized in one class but not another

六眼飞鱼酱① 提交于 2019-12-13 18:05:38

问题


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

回答1:


A baseball is not a bracelet. You declare your properties as:

@property (nonatomic, retain) NSArray *baseballArray;
@property (nonatomic, retain) Charm *selectedBaseball;

...and then you synthesize:

@synthesize braceletArray, selectedBracelet;

...without ever marking baseballArray and selectedBaseball as @dynamic (if you intend to specify their getters/setters yourself), and without actually declaring any properties called "braceletArray" and "selectedBracelet".

Also, a Charm is also not a Baseball. You define inconsistent getter and setter methods:

- (void)setSelectedBaseball:(Baseball *)theBaseball;
- (Charm*)getSelectedBaseball; 

It seems to me like perhaps you are porting some class that used to work with charms and bracelets and modifying it so that it works with baseballs, and that you tried to build it when it was only halfway modified to work with baseballs.

I suggest completing your modifications so that all the types and property name are consistent between your interface and your implementation, and then seeing if you still see the missing selector problem.



来源:https://stackoverflow.com/questions/6631940/objective-c-singleton-problem-object-recognized-in-one-class-but-not-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!