核心数据:无法访问由获取结果控制器返回的托管对象

发布于 2024-10-19 07:17:47 字数 11410 浏览 2 评论 0原文

我正在为 iPad 开发一个应用程序,但无法显示已保存在核心数据中的数据。我之前已经问过这个问题,很多人都帮助了我,但我仍然遇到同样的问题。这次,在调试时,我收到错误消息,无法访问我设置的用于接收所获取对象的数组。

我的代码如下:

    #import "RootViewController.h"
    #import "DetailViewController.h"
    #import "AddViewController.h"
    #import "EmployeeDetailsAppDelegate.h"

/*
 This template does not ensure user interface consistency during editing operations in the table view. You must implement appropriate methods to provide the user experience you require.
 */

@interface RootViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end



@implementation RootViewController

@synthesize detailViewController, fetchedResultsController, managedObjectContext, array, dictionary;
//@synthesize array;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {

    self.title = @"Employee Name";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [super viewDidLoad];

    if (self.fetchedResultsController) 
    {
        NSArray *tempArr = self.fetchedResultsController.fetchedObjects;
        NSLog(@"temp array is %@",tempArr);
        int count =[tempArr count];
        int i;
        for(i=0;i <count;i++)
        {
            NSManagedObject *category = [tempArr objectAtIndex:i];
            dictionary = [[NSMutableDictionary alloc] initWithCapacity:0];

            if([[category valueForKey:@"EmployeeName"] isKindOfClass:[NSString class]])
            {
                [dictionary setObject:[category valueForKey:@"EmployeeName"] forKey:@"EmployeeName"];
            }

            if([[category valueForKey:@"EmployeeID"] isKindOfClass:[NSString class]])
            {
                [dictionary setObject:[category valueForKey:@"EmployeeID"] forKey:@"EmployeeID"];
            }
            if([[category valueForKey:@"EmployeeDepartment"] isKindOfClass:[NSString class]])
            {
                [dictionary setObject:[category valueForKey:@"EmployeeDepartment"] forKey:@"EmployeeDepartment"];
            }

            [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"EmployeeName"];

        [self.array addObject:dictionary];

        [dictionary release];
        [tempArr release];
        }
    }
}




- (void)viewWillAppear:(BOOL)animated {

    [self.tableView reloadData];
//    [super viewWillAppear:animated];
}


/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

 */

/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
 */

/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
 */

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.    
    return YES;
}

#pragma mark -
#pragma mark Add a new object

- (void)insertNewObject:(id)sender {

    AddViewController *add = [[AddViewController alloc]initWithNibName:@"AddViewController" bundle:nil];
    self.modalPresentationStyle = UIModalPresentationFormSheet;
    add.wantsFullScreenLayout = NO;

    [self presentModalViewController:add animated:YES];
    [add release];  
}   


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

//  id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
//    return [sectionInfo numberOfObjects];
    if([self.array count])
    {
        return [array count];
    }
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.

//  [self configureCell:cell atIndexPath:indexPath];

    if(dictionary = [array objectAtIndex:indexPath.row])
    {
        [dictionary objectForKey:@"EmployeeName"];
        cell.textLabel.text = @"(EmployeeName = %@)";        
    }

    else {
        cell.textLabel.text = @"No Employee Name";
    }


    return cell;
}

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{

//  AddViewController *detail = [fetchedResultsController objectAtIndexPath:indexPath];
//  cell.textLabel.text = detail.empName.text;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the managed object.
        NSManagedObject *objectToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        if (self.detailViewController.detailItem == objectToDelete) {
            self.detailViewController.detailItem = nil;
        }

        NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
        [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

        NSError *error;
        if (![context save:&error]) {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }   
}


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // The table view should not be re-orderable.
    return NO;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Set the detail item in the detail view controller.

    detailViewController = [[DetailViewController alloc]initWithStyle:UITableViewStylePlain];
//  AddViewController *selectedName = (AddViewController *)[[self fetchedResultsController]objectAtIndexPath:indexPath];
//  detailViewController.detail = selectedName;
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}


#pragma mark -
#pragma mark Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    /*
     Set up the fetched results controller.
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Details" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"EmployeeName" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    AddViewController *name = [[AddViewController alloc]init];

    if (name.empName.text) {
        NSPredicate *inPredicate = [NSPredicate predicateWithFormat: @"EmployeeName = %@", name.empName.text];
        [fetchRequest setPredicate:inPredicate];
    }

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptors release];

    return fetchedResultsController;
}    


#pragma mark -
#pragma mark Fetched results controller delegate

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {

    self.fetchedResultsController = nil;
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;

//  self.array = nil;
}


- (void)dealloc {

//  [array release];
    [detailViewController release];
    [fetchedResultsController release];
    [managedObjectContext release];

    [super dealloc];
}

@end

I am developing an application for iPad but is unable to display the data already save in core data. I have asked this earlier and many people has helped me but still I am having the same problem. This time, while debugging I am getting the error that the array which I have set up to receive the fetched objects cannot be accessed.

My code is as follows:

    #import "RootViewController.h"
    #import "DetailViewController.h"
    #import "AddViewController.h"
    #import "EmployeeDetailsAppDelegate.h"

/*
 This template does not ensure user interface consistency during editing operations in the table view. You must implement appropriate methods to provide the user experience you require.
 */

@interface RootViewController ()
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end



@implementation RootViewController

@synthesize detailViewController, fetchedResultsController, managedObjectContext, array, dictionary;
//@synthesize array;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {

    self.title = @"Employee Name";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [super viewDidLoad];

    if (self.fetchedResultsController) 
    {
        NSArray *tempArr = self.fetchedResultsController.fetchedObjects;
        NSLog(@"temp array is %@",tempArr);
        int count =[tempArr count];
        int i;
        for(i=0;i <count;i++)
        {
            NSManagedObject *category = [tempArr objectAtIndex:i];
            dictionary = [[NSMutableDictionary alloc] initWithCapacity:0];

            if([[category valueForKey:@"EmployeeName"] isKindOfClass:[NSString class]])
            {
                [dictionary setObject:[category valueForKey:@"EmployeeName"] forKey:@"EmployeeName"];
            }

            if([[category valueForKey:@"EmployeeID"] isKindOfClass:[NSString class]])
            {
                [dictionary setObject:[category valueForKey:@"EmployeeID"] forKey:@"EmployeeID"];
            }
            if([[category valueForKey:@"EmployeeDepartment"] isKindOfClass:[NSString class]])
            {
                [dictionary setObject:[category valueForKey:@"EmployeeDepartment"] forKey:@"EmployeeDepartment"];
            }

            [dictionary setObject:[NSNumber numberWithInt:0] forKey:@"EmployeeName"];

        [self.array addObject:dictionary];

        [dictionary release];
        [tempArr release];
        }
    }
}




- (void)viewWillAppear:(BOOL)animated {

    [self.tableView reloadData];
//    [super viewWillAppear:animated];
}


/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

 */

/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
 */

/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
 */

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.    
    return YES;
}

#pragma mark -
#pragma mark Add a new object

- (void)insertNewObject:(id)sender {

    AddViewController *add = [[AddViewController alloc]initWithNibName:@"AddViewController" bundle:nil];
    self.modalPresentationStyle = UIModalPresentationFormSheet;
    add.wantsFullScreenLayout = NO;

    [self presentModalViewController:add animated:YES];
    [add release];  
}   


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

//  id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
//    return [sectionInfo numberOfObjects];
    if([self.array count])
    {
        return [array count];
    }
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.

//  [self configureCell:cell atIndexPath:indexPath];

    if(dictionary = [array objectAtIndex:indexPath.row])
    {
        [dictionary objectForKey:@"EmployeeName"];
        cell.textLabel.text = @"(EmployeeName = %@)";        
    }

    else {
        cell.textLabel.text = @"No Employee Name";
    }


    return cell;
}

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{

//  AddViewController *detail = [fetchedResultsController objectAtIndexPath:indexPath];
//  cell.textLabel.text = detail.empName.text;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the managed object.
        NSManagedObject *objectToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        if (self.detailViewController.detailItem == objectToDelete) {
            self.detailViewController.detailItem = nil;
        }

        NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
        [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

        NSError *error;
        if (![context save:&error]) {
            /*
             Replace this implementation with code to handle the error appropriately.

             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }   
}


- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // The table view should not be re-orderable.
    return NO;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // Set the detail item in the detail view controller.

    detailViewController = [[DetailViewController alloc]initWithStyle:UITableViewStylePlain];
//  AddViewController *selectedName = (AddViewController *)[[self fetchedResultsController]objectAtIndexPath:indexPath];
//  detailViewController.detail = selectedName;
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}


#pragma mark -
#pragma mark Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    /*
     Set up the fetched results controller.
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Details" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"EmployeeName" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    AddViewController *name = [[AddViewController alloc]init];

    if (name.empName.text) {
        NSPredicate *inPredicate = [NSPredicate predicateWithFormat: @"EmployeeName = %@", name.empName.text];
        [fetchRequest setPredicate:inPredicate];
    }

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptors release];

    return fetchedResultsController;
}    


#pragma mark -
#pragma mark Fetched results controller delegate

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {

    self.fetchedResultsController = nil;
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;

//  self.array = nil;
}


- (void)dealloc {

//  [array release];
    [detailViewController release];
    [fetchedResultsController release];
    [managedObjectContext release];

    [super dealloc];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙沙粒小 2024-10-26 07:17:47

首先,您不必要地创建一个字典数组,它只不过是复制已在获取结果控制器 fetchedObjects 中返回的数组。

您似乎想检查托管对象属性的空值,但您的做法是错误的。这种类型的检查:

if([[category valueForKey:@"EmployeeName"] isKindOfClass:[NSString class]])

... 即使属性没有值,也会评估为 TRUE,因为键值编码返回的值将是一个字符串(因为生成的访问器方法总是返回一个字符串。

处理空的最佳位置字段位于数据模型本身中。只需将属性 EmployeeName 的默认值设置为“No Employee Name”即可免除所有检查。

其次,您对数组使用了不一致的引用。有时您使用self.array,有时仅使用array,只有在使用第一种形式时您才会获得适当的保留,如果您使用第二种形式,您的数组可能会随机消失。 第三,

tableView:(UITableView *)tableView cellForRowAtIndexPath: 中,这行:

if(dictionary = [array objectAtIndex:indexPath.row])

... 使用了错误的访问形式,因此它可能会也可能找不到数组对象

cell.textLabel.text = @"(EmployeeName = %@)";

。将单元格的文本更改为“(EmployeeName =)”,并且可能会产生编译错误。它应该是:

    cell.textLabel.text = [NSString stringWithFormat:@"(EmployeeName = %@)",[dictionary objectForKey:@"EmployeeName"]];

您确实应该像这样使用获取的结果控制器和默认字段(如上所述):

NSManagedObject *mo=[[fetchedResultsController fetchedObjects] objectAtIndex:index.row]];   
    cell.textLabel.text = [NSString stringWithFormat:@"(EmployeeName = %@)",[mo valueForKey:@"EmployeeName"]];

您不必进行错误检查。因为表中的行数始终与获取的托管对象的数量相匹配。

我认为你让这件事变得比必要的复杂得多。您可能习惯于对 SQL 返回的数据进行大量完整性检查。但是,核心数据和获取的结果控制器将为您处理大部分工作。在这样的简单设置中,通常只需向 tableview 控制器模板添加大约十行代码。

Firstly, you are needlessly creating an array of dictionaries that does nothing more than duplicate the array already return in fetched results controllers fetchedObjects.

You seem to want to check for null values for managed object attributes but your going about it wrong. This type of check:

if([[category valueForKey:@"EmployeeName"] isKindOfClass:[NSString class]])

... will evaluate as TRUE even if the attribute as no value because value returned by key-value coding will be a string (because of the generated accessor method that always returns a string.

The best place to handle empty fields is in the data model itself. Just set the default value of the attribute EmployeeName to "No Employee Name" and you can dispense with all the checking.

Secondly, you are using inconsistent references to your array. Sometimes you use self.array and other times just array. You will get proper retention only when using the first form. If you use the second, your array may disappear at random.

Thirdly, in tableView:(UITableView *)tableView cellForRowAtIndexPath: this line:

if(dictionary = [array objectAtIndex:indexPath.row])

... uses the wrong access form so it may or may not find the array object. This line:

cell.textLabel.text = @"(EmployeeName = %@)";

... sets all the cell's text to "(EmployeeName =)" and will probably produce a compile error. It should be:

    cell.textLabel.text = [NSString stringWithFormat:@"(EmployeeName = %@)",[dictionary objectForKey:@"EmployeeName"]];

You really should just use the fetched results controller and default fields (as above) like this:

NSManagedObject *mo=[[fetchedResultsController fetchedObjects] objectAtIndex:index.row]];   
    cell.textLabel.text = [NSString stringWithFormat:@"(EmployeeName = %@)",[mo valueForKey:@"EmployeeName"]];

You don't have to do error checking because the number of row in the table always matches the number of fetched managed objects.

I think you are making this a lot more complicated than necessary. You are probably used to doing a lot of integrity checks with data returned by SQL. However, Core Data and the fetched results controller will handle most of that work for you. In a simple setup like this, you usually only have to add about ten lines of code to the tableview controller template.

一城柳絮吹成雪 2024-10-26 07:17:47

我不确定,但我认为您将元素获取到数组的方式失败了:

NSArray *tempArr = self.fetchedResultsController.fetchedObjects;

您可以尝试执行 for 循环并将每个对象添加到数组中。像这样的东西:

 NSMutableArray *tempArray = [[NSMutableArray alloc] init];

for(elementType *element in [self.fetchedResultsController fetchedObjects])
{
    [tempArray addObject: [element objectForKey: appropriateKey]];
}  

I'm not sure, but I think you the way you are getting the elements to the array fails:

NSArray *tempArr = self.fetchedResultsController.fetchedObjects;

You could try to do a for-loop and add each object into the array. Something like this:

 NSMutableArray *tempArray = [[NSMutableArray alloc] init];

for(elementType *element in [self.fetchedResultsController fetchedObjects])
{
    [tempArray addObject: [element objectForKey: appropriateKey]];
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文