0

所以当我运行模拟器时,我得到:

2013-01-28 13:35:38.271 Gas Index[79343:11303] -[Gas isEqualToString:]: unrecognized selector sent to instance 0x71a2260
2013-01-28 13:35:38.274 Gas Index[79343:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Gas isEqualToString:]: unrecognized   selector sent to instance 0x71a2260'

这是我的 ViewController.m 代码。任何帮助表示赞赏。(我是 xcode 的 n00b)

#import "ViewController.h"
#import "DetailViewController.h"


@interface ViewController ()

@end

@implementation ViewController {
    NSArray *gases;
}

@synthesize tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    Gas *gas1 = [Gas new];
    gas1.name=@"Argon";
    gas1.desc=@"An Inert Gas,Ar";

    Gas *gas2 = [Gas new];
    gas2.name=@"Carbon Dioxide";
    gas2.desc=@"An Inert Gas, CO2";

    Gas *gas3 = [Gas new];
    gas3.name=@"Nitrogen";
    gas3.desc=@"An Inert Gas, N2";

    gases = [NSArray arrayWithObjects:gas1,gas2,gas3, nil];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [gases count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"GasCell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.textLabel.text = [gases objectAtIndex:indexPath.row];
    return cell;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showGasDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        DetailViewController *destViewController = segue.destinationViewController;
        destViewController.gas = [gases objectAtIndex:indexPath.row];
    }
}
@end
4

1 回答 1

2

问题在于这一行:

cell.textLabel.text = [gases objectAtIndex:indexPath.row];

拆分该代码:

Gas *gas = gases[indexPath.row];
cell.textLabel.text = gas;

这就是你想要做的。您不能将Gas对象分配给需要NSString. 你可能想要这个:

Gas *gas = gases[indexPath.row];
cell.textLabel.text = gas.name;

请注意,当您在调试器中运行此程序时,您应该会看到一个堆栈跟踪,显示该错误发生在哪一行。

于 2013-01-28T19:10:27.287 回答