数据未写入NSMutableArray

时间:2013-09-07 22:10:27

标签: ios objective-c uitableview core-data nsmutablearray

我有一个iOS应用程序从服务器提取数据并使用CoreData持久保存。我有一个UITableView,我试图只填充给定核心数据属性的选择部分。

在填充表格之前,我循环访问数据并将我想要的内容传递给NSMutableArray。问题是,当我找到一个我想要的项目时,它没有被添加到数组中。

我在我的.h文件中声明了这样的数组......

@property (strong, nonatomic) NSMutableArray *theNewSource;

并在.m文件

中合成它
@synthesize theNewSource = _theNewSource;

这是方法......

-(NSMutableArray *)setDataSourceArray
{

    for(int i = 0; i < rcount ; i++)
    {
        NSIndexPath *countingInteger = [NSIndexPath indexPathForItem:i inSection:0];
        NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:countingInteger];
        NSString *action = [object valueForKey:@"theActionName"];

        if (![action isEqual:@"Login"])
        {            
            [_theNewSource addObject:action];
        }
    }
    NSLog(@"the array is now %@",_theNewSource);
    return _theNewSource;

}

我在第[_theNewSource addObject:action]行设置了一个断点。我可以在控制台中看到变量action确实有一个值,但它永远不会添加到_theNewSource数组中...我确定这是目标C 101但我无法弄清楚它出。请帮助!

2 个答案:

答案 0 :(得分:1)

您是否创建过_theNewSource阵列?看起来你没有做到以下几点:

_theNewSource = [[NSMutableArray alloc] init];

确保在尝试使用之前创建实例。

答案 1 :(得分:1)

您应该直接在NSFetchedResultsController的fetchRequest中使用谓词:

[NSPredicate predicateWithFormat:@"theActionName != %@", @"Login"];

NSFetchResultsControllers对于驱动表视图和集合视图特别有用,因此过滤它们的结果以创建单独的数据源是代码味道。

这样做意味着您可以直接使用NSFetchedResultsController作为表的数据源,而不是使用它来创建过滤数组作为数据源。

相关问题