我在单例中有一个 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/