1

我试图在按钮操作时交换两个对象,但我无法让它工作。它是第一次交换,但下次当我单击按钮时,它会因 EXC_BAD_ACCESS 而崩溃。

这是代码

-(IBAction)swapLocation
{
    CCLocDTO *tempDto = searchDTO.toDTO;
    searchDTO.toDTO = searchDTO.fromDTO;
    searchDTO.fromDTO = tempDto;

    NSLog(@"From Location is %@",searchDTO.fromDTO.streetAddress); //it becomes empty on next button click
    NSLog(@"To Location is %@",searchDTO.toDTO.streetAddress);
    NSLog(@"From Special Location is %@",searchDTO.fromSpecialDTO.locationName);
    NSLog(@"To Special Location is %@",searchDTO.toSpecialDTO.locationName);

    [self.tableView reloadData];

}

我在 Search DTO(searchDTO) 中分配 fromDTO 和 toDTO。

4

1 回答 1

4

如果你不使用ARC,则需要tempDto在设置为其他内容之前保留searchDTO.toDTO;否则旧值可能会被释放,并且您将在tempDto. 它应该如下所示:

CCLocDTO *tempDto = [searchDTO.toDTO retain];
searchDTO.toDTO = searchDTO.fromDTO;
searchDTO.fromDTO = [tempDto autorelease];

或像这样:

CCLocDTO *tempDto = [searchDTO.toDTO retain];
searchDTO.toDTO = searchDTO.fromDTO;
searchDTO.fromDTO = tempDto;
[tempDto release];
于 2012-08-08T05:26:45.783 回答