召回后,NSMutableArray部分为空

时间:2012-09-02 13:50:07

标签: objective-c nsmutablearray

我的视图控制器中有一个属性NSMutableArray

ContactsViewController.h:

@interface ContactsViewController : UIViewController <UITableViewDelegate,      UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray *contacts;
...
@end

在这个视图控制器中,我在“viewDidLoad”上填充我的数组

ContactsViewController.m:

@implementation ContactsViewController

@synthesize contacts;
...
...
- (void)viewDidLoad
{
  [super viewDidLoad];
  DBhandler *handler = [[DBhandler alloc] init];

  if    (contacts)
    [contacts removeAllObjects];
  else
    contacts = [[NSMutableArray alloc] init];    
  // Get all my contacts that are in my core data file
  // This function returns a NSMutableArray
  contacts=[handler getContacts];

//loop through contacts of addressbook when user wants that
if ([allContactSwitch isOn])
{

    //open link to addressbook
    ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
    CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );

    for( CFIndex personIndex = 0; personIndex < nPeople; personIndex++ ) {            
        ABRecordRef refVal = CFArrayGetValueAtIndex( allPeople, personIndex );            
        Boolean newContact = true;
        // check if contact is already in Core data File
        for( CFIndex i = 0; i < [contacts count]; i++ ) {
            contact *checkcontact=[contacts objectAtIndex:i];
            if (personIndex==checkcontact.personRef)
                newContact = FALSE;
        }
        if (newContact)
        {
            contact *dummycontact = [[contact alloc]init];
            dummycontact.personRef = personIndex;
            dummycontact.contactName = (__bridge NSString *)(ABRecordCopyCompositeName( refVal ));
            // Add contact to array 
            [contacts addObject:dummycontact];
        }
    }
}
// Just to check, the entire array looks fine!  
for( CFIndex i = 0; i < [contacts count]; i++ ) {
    contact *dummycontact=[contacts objectAtIndex:i];
    NSLog(@"Name after build: %@", dummycontact.contactName);
}

}

但是后来填充表视图的不同单元格时,来自[handle getContacts]的NSMutableArray部分为空:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"cell number %i",indexPath.row);
static NSString *CellIdentifier = @"Cell";

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

// Set up the cell...
contact *dummycontact=[contacts objectAtIndex:indexPath.row];
// Only part of the objects in the array contacts contain data!
NSLog(@"Name cell: %@ %i", dummycontact.contactName, indexPath.row);

cell.textLabel.text = dummycontact.contactName;

return cell;
}

这可能与此同时清除[handle getContacts]中创建的对象的内存这一事实有关。但我不知道如何解决这个问题。我已经尝试克隆或复制[handle get contacts]的输出,但我没有成功。 要完成函数“getContacts”:

-(NSMutableArray*)getContacts{

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

NSManagedObjectContext *context = [self managedObjectContext];
DBcontact *contact = [NSEntityDescription
                      insertNewObjectForEntityForName:@"WhappContacts"
                      inManagedObjectContext:context];
// Test listing all FailedBankInfos from the store
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WhappContacts"
                                          inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (contact in fetchedObjects) {
   [contacts addObject:contact];
   // Data seems fine here.
   NSLog(@"Name in: %@", contact.contactName);  
}
return contacts;
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

另一种方法是将联系信息放在字典中:

- (NSMutableArray*)getContacts {
    NSMutableArray *contacts = [[NSMutableArray alloc] init];

    NSManagedObjectContext *context = [self managedObjectContext];
    DBcontact *contact = [NSEntityDescription insertNewObjectForEntityForName:@"WhappContacts" inManagedObjectContext:context];
    // Test listing all FailedBankInfos from the store
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WhappContacts" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];
    NSError *error;
    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (contact in fetchedObjects) {
        NSDictionary *contactDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                contact.contactName, @"contactName", nil]; //add other necessary contact information

        [contacts addObject:contactDict];
        // Data seems fine here.
        NSLog(@"Name in: %@", contact.contactName);  
    }

    return contacts;
}

并检索信息:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //NSLog(@"cell number %i",indexPath.row);
    static NSString *CellIdentifier = @"Cell";

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

    // Set up the cell...
    NSDictionary *dummycontact = [contacts objectAtIndex:indexPath.row];
    // Only part of the objects in the array contacts contain data!
    NSLog(@"Name cell: %@ %i", [dummycontact objectForKey:@"contactName"], indexPath.row);

    cell.textLabel.text = [dummycontact objectForKey:@"contactName"];

    return cell;
}