use*_*675 1 iphone xcode singleton objective-c singleton-methods
我的问题是我可以从一个类中的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) {
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
Run Code Online (Sandbox Code Playgroud)
棒球不是手镯.您将属性声明为:
@property (nonatomic, retain) NSArray *baseballArray;
@property (nonatomic, retain) Charm *selectedBaseball;
Run Code Online (Sandbox Code Playgroud)
...然后你合成:
@synthesize braceletArray, selectedBracelet;
Run Code Online (Sandbox Code Playgroud)
......而没有标记baseballArray
,并selectedBaseball
为@dynamic
(如果你打算指定自己的getter/setter方法自己),并没有实际声明称为"braceletArray"和"selectedBracelet"任何属性.
另外,a Charm
也不是Baseball
.您定义了不一致的getter和setter方法:
- (void)setSelectedBaseball:(Baseball *)theBaseball;
- (Charm*)getSelectedBaseball;
Run Code Online (Sandbox Code Playgroud)
在我看来,也许你正在移植一些曾经与魅力和手镯一起工作的类并修改它以便它与棒球一起使用,并且当你只修改一半以使用棒球时,你试图建立它.
我建议您完成修改,以便所有类型和属性名称在您的界面和实现之间保持一致,然后查看是否仍然看到缺少的选择器问题.
归档时间: |
|
查看次数: |
1429 次 |
最近记录: |