我的问题是我可以从一个类中的sharedInstance Singleton访问方法和属性,但我不能在另一个类中访问。
例如,下面的代码可以工作并被X代码识别。工作良好。 return [[[SINGLETON sharedInstance] baseballArray] count];
除了:
theSelectedBaseball = [[[SINGLETON sharedInstance]baseballArray]objectAtIndex:indexPath.row];
SINGLETON *singleton = [SINGLETON sharedInstance];
[singleton setSelectedBaseball:theSelectedBaseball];
但是,如果我在另一个类中尝试上面的代码,我会收到以下警告消息: - Method - setSelectedBaseball:not found。
我在要使用它的所有类中导入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
实现:
#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
答案 0 :(得分:6)
棒球不是手镯。您将属性声明为:
@property (nonatomic, retain) NSArray *baseballArray;
@property (nonatomic, retain) Charm *selectedBaseball;
...然后你合成:
@synthesize braceletArray, selectedBracelet;
...没有将baseballArray
和selectedBaseball
标记为@dynamic
(如果您打算自己指定其getter / setter),并且实际上没有声明任何名为“braceletArray”的属性, “selectedBracelet”。
此外,Charm
也不是Baseball
。您定义了不一致的getter和setter方法:
- (void)setSelectedBaseball:(Baseball *)theBaseball;
- (Charm*)getSelectedBaseball;
在我看来,或许你正在移植一些曾经使用过魅力和手镯并修改它以便它适用于棒球的类,并且当你只修改了一半用于棒球时,你试图建立它。
我建议您完成修改,以便所有类型和属性名称在您的界面和您的实现之间保持一致,然后查看您是否仍然看到缺少的选择器问题。