iOS 5.0 - 将Helper类的NSMutableArray传递给View Controller

时间:2012-03-21 19:08:11

标签: objective-c ios ios5 nsmutablearray

我有一个帮助类,旨在从Core Data获取数据并将其传递回视图控制器。这是代码:

查看控制器

@interface AnnouncementsController : UIViewController

@property (nonatomic, retain) NSMutableArray *announcements;

@end

@implementation AnnouncementsController

@synthesize announcements;

- (void)viewDidLoad
{
    AnnouncementCoreDataHelper *announcementHelper = [[AnnouncementCoreDataHelper alloc] init];
    self.announcements = [announcementHelper selectAnnouncementsWithPredicate:@"isActive = 1" sortDescriptor:@"lastUpdated" sortAscending:NO];

    [super viewDidLoad];
}

@end

Core Data Helper 实施:

// Select one or more announcements based on a predicate if passed in and a sort order
- (NSMutableArray*)selectAnnouncementsWithPredicate:(NSString *)predicateString sortDescriptor:(NSString *)sortBy sortAscending:(BOOL)isAscending
{
    NSError *error = nil;

    // Build the entity and request
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Announcement" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entity];

    if(predicateString)
    {
        // Set the search criteria
        NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
        [request setPredicate:predicate];
    }

    if(sortBy)
    {
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortBy ascending:isAscending];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
        [request setSortDescriptors:sortDescriptors];
    }

    // Perform the search
    NSMutableArray *results = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    if(results == Nil)
    {
        NSLog(@"%@", error);
    }

    return results;    
}

问题是Core Data Helper在其NSMutableArray结果中返回两个对象,但View Controller的NSMutableArray公告为零。将NSMutableArray从帮助器传递回控制器后会有什么损失?如果我将所有变量和结果更改为NSArray,一切正常。

1 个答案:

答案 0 :(得分:0)

我相信这是Xcode的一个时髦问题。我清理并重新构建,一切正常。我为浪费你的时间而感到抱歉,感谢您的帮助!我想更多地了解内存管理问题。