Objective-C Singleton问题。在一个类中识别的对象,而不是另一个类

时间:2022-09-07 07:42:24

My problem is that I can access methods and attributes from the sharedInstance Singleton in one class but I cannot in another.

我的问题是我可以从一个类中的sharedInstance Singleton访问方法和属性,但我不能在另一个类中访问。

For example, the code below works and is recognized by X-code. Works fine. return [[[SINGLETON sharedInstance]baseballArray]count];

例如,下面的代码可以工作,并由X代码识别。工作正常。 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.

但是,如果我在另一个类中尝试上面的代码,我会收到以下警告消息: - 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.

我在所有要使用它的类中导入SINGLETON标头。我仔细观察了它被认可的课程,而另一课则认为它不是,我无法弄清楚为什么它不被认可。

Here is my Singleton Class.

这是我的Singleton类。

#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 个解决方案

#1


6  

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

...没有将baseballArray和selectedBaseball标记为@dynamic(如果您打算自己指定他们的getter / setter),并且实际上没有声明任何名为“braceletArray”和“selectedBracelet”的属性。

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

此外,魅力也不是棒球。您定义了不一致的getter和setter方法:

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

我建议您完成修改,以便所有类型和属性名称在您的界面和实现之间保持一致,然后查看是否仍然看到缺少的选择器问题。

#1


6  

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

...没有将baseballArray和selectedBaseball标记为@dynamic(如果您打算自己指定他们的getter / setter),并且实际上没有声明任何名为“braceletArray”和“selectedBracelet”的属性。

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

此外,魅力也不是棒球。您定义了不一致的getter和setter方法:

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

我建议您完成修改,以便所有类型和属性名称在您的界面和实现之间保持一致,然后查看是否仍然看到缺少的选择器问题。