1

我有一个 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数组中……我确定这是 Objective C 101,但我无法弄清楚。请帮忙!

4

2 回答 2

1

你甚至创建了你的_theNewSource数组吗?您似乎没有执行以下操作:

_theNewSource = [[NSMutableArray alloc] init];

在尝试使用它之前,请确保您正在创建您的实例。

于 2013-09-07T22:13:48.760 回答
1

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

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

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

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

于 2013-09-08T02:22:26.007 回答