2016-01-20 27 views
0

我很抱歉,我不擅长英语,可能无法清楚地说明问题。然后代码是:NSMutableDictionary <NSString *,NSMutableArray *>

我声明的属性作为我的数据源

@property (nonatomic, strong) NSMutableDictionary<NSString*,NSMutableArray*> *sectionData; 

和初始化像self.sectionData[fir] = [NSMutableArray array];确保有字典中的一个NSMutableArray对象。但是当我获取数据,如

NSMutableArray *subArray =self.sectionData[self.sectionNames[indexPath.section]]; 
NSLog(@"%@",NSStringFromClass([subArray class])); 

控制台显示:__NSArrayI
为什么出现这种情况,为什么不NSMutableArray的,现在我不能在数据源中删除某些项目,

的代码我添加初始化我的数据源是:

- (NSMutableArray *)sectionNames 
{ 
    if (!_sectionNames) { 
     _sectionNames = [NSMutableArray array]; 
     self.sectionData = [NSMutableDictionary dictionary]; 
     NSString *sourceStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"states" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil]; 

     NSArray *states = [sourceStr componentsSeparatedByString:@"\n"]; 
     [states enumerateObjectsUsingBlock:^(NSString * str, NSUInteger idx, BOOL * _Nonnull stop) { 

      if ([str isNotNullOrWhiteSpace]) { 
       unichar chars[] = {[str characterAtIndex:0]}; 
       NSString *fir = [[NSString stringWithCharacters:chars length:1] uppercaseString]; 

       if (![_sectionNames containsObject:fir] && ![fir isEqualToString:@""]) { 

        [_sectionNames addObject:[fir uppercaseString]]; 
        self.sectionData[fir] = [NSMutableArray array]; 
       } 
       [self.sectionData[fir] addObject:str]; 
      } 
     }]; 

     //对sectionData中的array元素对象进行排序 
     __weak typeof(self) weakSelf = self; 
     [self.sectionData enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSMutableArray *obj, BOOL * _Nonnull stop) { 

      weakSelf.sectionData[key] = [[obj sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { 

       return [[obj1 uppercaseString] compare:[obj2 uppercaseString]]; 
      }] copy]; 
     }]; 

    } 
    return _sectionNames; 
} 

,然后我删除一些选定的项目:

- (void)doDelete:(UIBarButtonItem*)deleteBtn 
{ 
    NSArray *deletedIndexpathes = [self.collectionView indexPathsForSelectedItems]; 
    if (deletedIndexpathes.count == 0) return; 
    //sort 
    deletedIndexpathes = [deletedIndexpathes sortedArrayUsingSelector:@selector(compare:)]; 
    deletedIndexpathes = [[deletedIndexpathes reverseObjectEnumerator]allObjects]; 

    //delete data 
    NSMutableIndexSet *emptySections = [NSMutableIndexSet indexSet]; // keep track of what sections get emptied 

    [deletedIndexpathes enumerateObjectsUsingBlock:^(NSIndexPath * indexPath, NSUInteger idx, BOOL * _Nonnull stop) { 

     NSMutableArray *subArray =(NSMutableArray *)self.sectionData[self.sectionNames[indexPath.section]]; 

     NSLog(@"%@",NSStringFromClass([subArray class])); 
//  [ subArray removeObjectAtIndex: indexPath.item]; 

//  NSLog(@"%@",self.sectionData[self.sectionNames[indexPath.section]]); 
     //如果一个section里面没有item,追踪下来 
//  if (cArray.count == 0) { 
////   [emptySections addObject:[NSNumber numberWithInteger:indexPath.section]]; 
//   [emptySections addIndex:indexPath.section]; 
//  } 
    }]; 

但子阵列一个NSArray,我cann`t删除....

+0

'[aa class]',你的意思是'[subArray class]'?你可以做'self.sectionData [fir] = [NSMutableArray array];'但是你在哪里设置数据? – Larme

+0

哦,它是[subArray类]。数据已在getter方法中设置.. – Evoque

+0

如何添加数据?例如,如果做'self.sectionData [fir] = [NSMutableArray array];'然后做'self.sectionData [fir] = @ [dataFetch1,dataFetch2];'你在那里给一个'NSArray'。 – Larme

回答

0

更换

weakSelf.sectionData[key] = [[obj sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { 

       return [[obj1 uppercaseString] compare:[obj2 uppercaseString]]; 
      }] copy]; 

weakSelf.sectionData[key] = [[obj sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { 

      return [[obj1 uppercaseString] compare:[obj2 uppercaseString]]; 
     }] mutableCopy]; 

copy - >mutableCopy

+0

哇,谢谢你这么这么... ...太.... !!!!! – Evoque

相关问题