0

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

4 回答 4

1

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

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];
于 2013-08-13T08:05:16.873 回答
0

I have a question with regards to the line below in your code:

if ([[StoreVars sharedInstance] respondsToSelector:@selector(sharedArrayOfVideo)])

Q. Why have you tried to check the mutableArray as method via selector ?

If you want to iterate through the added dictionaries do the following

StoreVars *storeVars = [StoreVars sharedInstance];
if(storeVars.sharedArrayOfVideo.count>0)
{
   for(int i=0; i<storeVars.sharedArrayOfVideo.count; i++)
   {
      NSDictionary *dictionary = [storeVars.sharedArrayOfVideo objectAtIndex:i];

      // do stuff with the grabbed dictionary.
   }
}
else { NSLog(@"sharedArrayOfVideo is empty"); }

Further in your singleton creation code perform the following edit:

Instead of following:

if (myInstance == nil) {
        myInstance = [[[[self class] alloc] init] retain];

Use the following:

if (myInstance == nil) {
        myInstance = [[StoreVars alloc] init]; 
        // you already own the above object via alloc/init, so no extra retains.
于 2013-08-13T07:24:53.380 回答
0

尝试使用 gcd 创建单例,如下所示:

static ASBTLEManager *sharedInstance = nil;

+ (ASBTLEManager *)sharedManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[ASBTLEManager alloc] initInstance];
    });
    return sharedInstance;
}
于 2013-08-13T07:27:03.677 回答
0

尝试以这种方式创建单例实例,这比您尝试的更准确

+(StoreVars *) sharedInstance
    {
        static StoreVars * myInstance = nil;
        static dispatch_once_t oneTimePredicate;

        //get calls only once.
        dispatch_once(&oneTimePredicate, ^{
            myInstance = [[self alloc] init];
            myInstance.sharedArrayOfVideo = [[NSMutableArray alloc] init];
        });
        return myInstance;
    }
于 2013-08-13T07:34:12.867 回答