Objective-C单例问题.在一类中被识别的对象,但另一类中未被识别的对象 [英] Objective-C Singleton problem. Object recognized in one class but not another
问题描述
我的问题是我可以在一个类中从sharedInstance Singleton访问方法和属性,但不能在另一个类中访问.
My problem is that I can access methods and attributes from the sharedInstance Singleton in one class but I cannot in another.
例如,以下代码有效,并且可以被X代码识别.工作良好.return [[[SINGLETON sharedInstance] baseballArray] count];
For example, the code below works and is recognized by X-code. Works fine. return [[[SINGLETON sharedInstance]baseballArray]count];
除了:
theSelectedBaseball = [[[SINGLETON sharedInstance]baseballArray]objectAtIndex:indexPath.row];
SINGLETON *singleton = [SINGLETON sharedInstance];
[singleton setSelectedBaseball:theSelectedBaseball];
但是,如果我在另一个类中尝试上面的代码,则会收到此警告消息:-方法-setSelectedBaseball:未找到.
However, if I try the code above in another class I get this warning message: - Method - setSelectedBaseball: not found.
我正在所有要使用它的类中导入SINGLETON标头.我仔细查看了被识别的类与未被识别的其他类,因此我无法弄清为什么它未被识别.
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.
这是我的单例课程.
#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
推荐答案
棒球不是手镯.您将属性声明为:
A baseball is not a bracelet. You declare your properties as:
@property (nonatomic, retain) NSArray *baseballArray;
@property (nonatomic, retain) Charm *selectedBaseball;
...然后合成:
@synthesize braceletArray, selectedBracelet;
...没有将 baseballArray
和 selectedBaseball
标记为 @dynamic
(如果您打算自己指定其获取者/设置者),并且而不实际声明任何名为"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".
此外, Charm
也不是 Baseball
.您定义不一致的getter和setter方法:
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.
这篇关于Objective-C单例问题.在一类中被识别的对象,但另一类中未被识别的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!