我的问题是我可以从一个类中的sharedInstance Singleton访问方法和属性,但我不能在另一个类中访问.
例如,下面的代码可以工作,并由X代码识别.工作良好.return [[[SINGLETON sharedInstance] baseballArray] count];
此外:
theSelectedBaseball = [[[SINGLETON sharedInstance]baseballArray]objectAtIndex:indexPath.row];
SINGLETON *singleton = [SINGLETON sharedInstance];
[singleton setSelectedBaseball:theSelectedBaseball];
Run Code Online (Sandbox Code Playgroud)
但是,如果我在另一个类中尝试上面的代码,我会收到以下警告消息: - Method - setSelectedBaseball:not found.
我在所有要使用它的类中导入SINGLETON标头.我仔细观察了它正在被认可的课程而不是其他课程,但是我无法弄清楚为什么它没有得到认可.
这是我的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
Run Code Online (Sandbox Code Playgroud)
执行:
#import "SINGLETON.h"
#import "Baseball.h"
@implementation SINGLETON
@synthesize baseballArray, selectedBaseball;
static SINGLETON *instance = nil;
+ (SINGLETON*)sharedInstance
{
@synchronized(self) { …Run Code Online (Sandbox Code Playgroud)