我是xCode的新手,在解决这个错误的问题时遇到了问题:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CharacterObject copyWithZone:]: unrecognized selector sent to instance 0x92606b0'
编译时没有报告错误。错误是" SIGABIT"并出现在这一行:
cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
这是我的代码。 " CharacterObject"是NSObject
文件。
@interface CharacterTableViewController ()
{
NSMutableArray *mycharacterArray;
NSMutableArray *myguestArray;
NSString *mainCharacters;
NSString *guestCharacters;
}
- (void)viewDidLoad
{
[super viewDidLoad];
mysections =[[NSArray alloc]init];
mycharacterArray = [[NSMutableArray alloc]init];
myguestArray = [[NSMutableArray alloc]init];
CharacterObject *myobject = [[CharacterObject alloc]init];
guestObject *myguests = [[guestObject alloc]init];
myheaders = [[NSArray alloc]initWithObjects:@"Main Characters", @"Guest Characters", nil];
mysections = [NSArray arrayWithObjects:mycharacterArray, myguestArray, nil];
myobject.charName = @"Joe Bloggs";
myobject.charPic = @"RB";
myobject.charNotes = @"Hello World";
[mycharacterArray addObject:myobject];
myguests = [[guestObject alloc]init];
myguests.charName = @"Jonny Boy";
myguests.charPic = @"JB";
myguests.charNotes = @"Jonny Boy was here";
[myguestArray addObject:myguests];
mysections = [NSArray arrayWithObjects:mycharacterArray, myguestArray, nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [mysections count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [myheaders objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[mysections objectAtIndex:section]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"mycell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return cell;
}
@end
提前感谢您的帮助。有任何问题请告诉我,我会尝试回答。
Phil W。
答案 0 :(得分:1)
该错误意味着您的类对象" CharacterObject"接到方法的调用" copyWithZone"并且它没有实现该方法。
当调用方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
时,它会尝试填充单元格,并在包含以下内容的行中填充:
cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
您正在尝试将CharacterObject强制转换为NSString(即第一次运行cellForRowAtIndexPath时,它使用indexPath(第0部分,第0行) - 这反过来意味着在行中:
cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
第一部分:
[mysections objectAtIndex:indexPath.section]
将返回包含myobject
的数组,第二部分objectAtIndex:indexPath.row]
将获取索引0处的对象(实际myobject
)。
要解决此问题,请更改:
cell.textLabel.text = (NSString *)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
为:
cell.textLabel.text = ((CharacterObject*)[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]).charName;
将从CharacterObject获取属性charName
。 请注意,这也将转换您的guestObject类,这将是一个问题。您应该测试[[mysections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
返回的对象的类类型,或者让两个对象继承自实现所需公共方法/属性的类。