皆さん、私は非常に単純な 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 という例外が発生します
objectAtIndex メッセージが NSCFSet オブジェクトに送信される理由がよくわかりません。シングルトンが間違って実装されているように感じます。前述のスレッドでアップルが推奨するような、より複雑なシングルトン実装も試しましたが、同じ問題がありました。あなたが提供できる洞察に感謝します。