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