ios - 单例 - 错误 : [CFString isNSString__] message sent to deallocated instance

标签 ios objective-c singleton

我在单例中有一个 NSMutableArray,我想在两个类中访问它。我在另一个项目中完成了这一点,与此完全相同(那个项目有 ARC,这个没有)并且它在那里工作。

项目未启用 ARC。

我收到错误:

*** -[CFString isNSString__]: message sent to deallocated instance 0xb3d1db0

StoreVars.h

@interface StoreVars : NSObject
@property (nonatomic, retain) NSMutableArray *sharedArrayOfVideo;
+ (StoreVars *)sharedInstance;
@end

StoreVars.m

@implementation StoreVars
@synthesize sharedArrayOfVideo;
+ (StoreVars *) sharedInstance {
    static StoreVars *myInstance = nil;
    if (myInstance == nil) {
        myInstance = [[[[self class] alloc] init] retain];
        myInstance.sharedArrayOfVideo = [[NSMutableArray alloc] init];
    }
    return myInstance;
}
@end

异步填充数组:

NSDictionary *tempDict = [[NSDictionary alloc] initWithObjectsAndKeys:
                                      ID,@"id",
                                      title,@"title", nil];
[[StoreVars sharedInstance].sharedArrayOfVideo addObject:tempDict];

这是发生崩溃的地方:

 NSLog(@"%i",[[StoreVars sharedInstance].sharedArrayOfVideo count]);
 NSLog(@"%@",[StoreVars sharedInstance]);
 if ([[StoreVars sharedInstance] respondsToSelector:@selector(sharedArrayOfVideo)]) {
        **NSLog(@"%@",[StoreVars sharedInstance].sharedArrayOfVideo);**
       //NSLog(@"%@",[[StoreVars sharedInstance].sharedArrayOfVideo objectAtIndex:8]);
}

输出:

10
<StoreVars: 0xb381b00>
*** -[CFString isNSString__]: message sent to deallocated instance 0xb3d1db0

最佳答案

发现问题,在创建字典时,我这样做了:

NSString *ID = [[[arrayOfEntry objectAtIndex:index] objectForKey:@"id"]; autorelease]

NSString *title = [[[arrayOfEntry objectAtIndex:index] objectForKey:@"title"] autorelease];

代替:

 NSString *ID = [[arrayOfEntry objectAtIndex:index] objectForKey:@"id"];

 NSString *title = [[arrayOfEntry objectAtIndex:index] objectForKey:@"title"];


 NSDictionary *tempDict = [NSDictionary dictionaryWithObjectsAndKeys:ID,@"id",title,@"title", nil];

关于ios - 单例 - 错误 : [CFString isNSString__] message sent to deallocated instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18202843/

相关文章:

ios - UIImageView 没有正确更新边界

ios - 无法声明另一个窗口

ios - 如何列出iPhone的pdf文件?

objective-c - 禁用 NSView mousedown

class - 如何在 AOP(面向方面​​的编程)中使用单例类?

Java:注入(inject)单例bean进行测试

java - Java 给定运行时存储中的持久存储

ios - 手机间隙 : cannot open iOS 6 native maps application

ios - 用于本地存储和显示服务器临时数据的核心数据 - 什么是最佳设计

iphone - TTSearchTextFieldDelegate 不调用 textField :didSelectObject: