1

私はいくつかのデータを保存するためにシングルトンを使用しています。彼女は実装です

 static ApplicationData *sharedData = nil;
 @implementation ApplicationData
 @synthesize list;

 + (id)sharedData
 {
  static dispatch_once_t dis;
  dispatch_once(&dis, ^{
     if (sharedData == nil) sharedData = [[self alloc] init];
 });
 return sharedData;
 }

  - (id)init
   {
   if (self = [super init])
   {
     list = [[NSMutableArray alloc]init];
   }
    return self;
   }

リストのオブジェクトが 3 つ未満 (2<) の場合、アプリは「空の配列の境界を超えたインデックス 0」でクラッシュします

   // NSMutableArray *anArray = [[NSMutableArray alloc]initWithObjects:@"", nil];
 while ([[[ApplicationData sharedData]list] lastObject] != nil)
{
    File *file = [[[ApplicationData sharedData]list] lastObject];

    BOOL isDir;
    if (![[NSFileManager defaultManager] fileExistsAtPath:file.filePath isDirectory:&isDir])
        {
            NSMutableDictionary *tmpDic = [NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:file.fileName,file.filePath,logEnteryErrorfileNotFoundDisplayName,[formatter stringFromDate:[NSDate date]], nil] forKeys:[NSArray arrayWithObjects:logShredFileName,logShredFilePath,logShredStatue,logShredDate, nil]];

            [logArray addObject:tmpDic];

            errorOccured = YES;
            [[[ApplicationData sharedData]list] removeLastObject];
            continue;
        }
   ... other code

  }

完全に機能するanArrayを使用すると。何が問題ですか ?

4

1 に答える 1

1

That's totally weird, you've probably did something else to achieve this. Why don't you use - (void)removeAllObjects?

Maybe you remove objects in the while cycle the last line, ie:

while ([[[ApplicationData sharedData]list] count] != 0)
{
    // remove object from list
    // ...
    [[[ApplicationData sharedData]list] removeLastObject];
}

And just a note, you don't need to check if (sharedData == nil) in sharedData as far as it's guaranteed to be executed only once. (unless you do something outside to your static variable, but that's not how it's supposed to be done I believe)

于 2012-11-07T10:45:48.700 に答える