大家好 - 我正在编写一个相当简单的iPhone应用程序。 数据来自于一个plist文件(基本上是NSDictionary),我试图将其加载到一个单例类中,并在我的各种视图控制器之间使用它来访问数据。
这是我的单例实现(严重模仿此线程):
@implementation SearchData
@synthesize searchDict;
@synthesize searchArray;
- (id)init {
if (self = [super init]) {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"searches.plist"];
searchDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
searchArray = [searchDict allKeys];
}
return self;
}
- (void)dealloc {
[searchDict release];
[searchArray release];
[super dealloc];
}
static SearchData *sharedSingleton = NULL;
+ (SearchData *)sharedSearchData {
@synchronized(self) {
if (sharedSingleton == NULL)
sharedSingleton = [[self alloc] init];
}
return(sharedSingleton);
}
@end
因此,每当我尝试在我的应用程序其他地方(如TableView代理)访问searchDict或searchArray属性时,就会像这样:
[[[SearchData sharedSearchData] searchArray] objectAtIndex:indexPath.row]
我遇到了一个异常,异常信息是 *** -[NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x5551f0
我不确定为什么会向 NSCFSet 对象发送 objectAtIndex 消息,感觉我的单例模式实现可能有问题。我也尝试过像苹果在上述帖子中推荐的更复杂的单例模式实现,但是仍然遇到了同样的问题。感谢您提供的任何见解。