简体   繁体   English

Singleton在Objective-C中共享数据源

[英]Singleton shared data source in Objective-C

Hey folks - I'm writing a pretty simple iPhone application. 嘿伙计们 - 我正在写一个非常简单的iPhone应用程序。 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. 数据来自plist文件(基本上是NSDictionary),我正在尝试加载到单例类中,并使用我的各种视图控制器来访问数据。

Here's the implementation for my singleton (heavily modeled after this thread ) 这是我的单例的实现(在此线程之后重新建模)

@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

So whenever I try to access the searchDict or searchArray properties elsewhere in my application (like a TableView delegate) like so: 所以每当我尝试访问我的应用程序中的其他地方的searchDict或searchArray属性时(如TableView委托),如下所示:

[[[SearchData sharedSearchData] searchArray] objectAtIndex:indexPath.row]

I get an exception stating *** -[NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x5551f0 我得到一个异常说明*** - [NSCFSet objectAtIndex:]:无法识别的选择器发送到实例0x5551f0

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. 我不确定为什么objectAtIndex消息被发送到NSCFSet对象,我觉得我的单例实现错误或其他什么。 I also tried a more complex singleton implementation like the one recommended by apple in the aforementioned thread and had the same problem. 我也尝试了一个更复杂的单例实现,就像上面提到的线程中苹果推荐的那样,并且遇到了同样的问题。 Thanks for any insight you can provide. 感谢您提供的任何见解。

In your -init method you are directly accessing your instance variables and you are not retaining them. 在您的-init方法中,您直接访问实例变量,而不是保留它们。 They're getting deallocated and their memory is being used up by other objects later on in your application's lifetime. 它们正在被解除分配,并且它们的内存在应用程序的生命周期中被其他对象用完。

Either retain your objects that you're creating there or use the non-convenience methods to generate them. 保留您在那里创建的对象,或使用非便利方法生成它们。

searchDict = [[NSDictionary alloc] initWithContentsOfFile:finalPath];
searchArray = [[searchDict allKeys] retain];

Whenever you assign synthesized variables, do it through 'self', so: 每当你分配合成变量时,通过'self'来做,所以:

- (id)init {
  if (self = [super init]) {
      NSString *path = [[NSBundle mainBundle] bundlePath];
      NSString *finalPath = [path stringByAppendingPathComponent:@"searches.plist"];
      self.searchDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
      self.searchArray = [searchDict allKeys];
  }

  return self;

} }

Also make sure you've set up those variables to be 'retain'ed in the header file. 还要确保您已将这些变量设置为在头文件中“保留”。

Hi, Can you tell me what is the advantage, when we assign synthesized variables through 'self'? 嗨,当我们通过'self'分配合成变量时,你能告诉我什么是优势? Thank you shiva 谢谢湿婆

the values are set through the setter; 值通过setter设置; it releases the previous value and retains the one you assign. 它会释放先前的值并保留您指定的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM