Note that there are some explanatory texts on larger screens.

plurals
  1. POSingleton shared data source in Objective-C
    text
    copied!<p>Hey folks - I'm writing a pretty simple iPhone application. The data comes from a plist file (NSDictionary basically), that I'm trying to load into a singleton class and use across my various view controllers to access the data.</p> <p>Here's the implementation for my singleton (heavily modeled after <a href="https://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like">this thread</a>)</p> <pre><code>@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 </code></pre> <p>So whenever I try to access the searchDict or searchArray properties elsewhere in my application (like a TableView delegate) like so:</p> <pre><code>[[[SearchData sharedSearchData] searchArray] objectAtIndex:indexPath.row] </code></pre> <p>I get an exception stating *** -[NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x5551f0</p> <p>I'm not really sure why the objectAtIndex message is being sent to an NSCFSet object, I feel like my singleton is implemented wrong or something. I also tried a more complex singleton implementation like the one recommended by apple in the <a href="https://stackoverflow.com/questions/145154/what-does-your-objective-c-singleton-look-like">aforementioned thread</a> and had the same problem. Thanks for any insight you can provide.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload