Objective c NSMutableArray在调用后部分为空
我的视图控制器中有一个属性NSMutableArray 联系人VIEWCONTROLLER.h:Objective c NSMutableArray在调用后部分为空,objective-c,nsmutablearray,Objective C,Nsmutablearray,我的视图控制器中有一个属性NSMutableArray 联系人VIEWCONTROLLER.h: @interface ContactsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> @property (nonatomic,strong) NSMutableArray *contacts; ... @end 这可能与[handle getContacts]中创
@interface ContactsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *contacts;
...
@end
这可能与[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) {
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;
}
另一种方法是将联系人信息放在字典中:
- (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;
}
您使用的是ARC还是预弧代码?您使用的是ARC还是预弧代码?
- (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;
}