簡體   English   中英

iOS模擬器錯誤Xcode 4.5

[英]iOS simulator error Xcode 4.5

所以,當我運行模擬器時,我得到:

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

問題出在這一行:

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;

請注意,當您在調試器中運行它時,您應該看到一個堆棧跟蹤,顯示您發生此錯誤的確切行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM