1

我是Objective-c的新手,我已经完成了一个为iPhone构建“小费计算器”应用程序的教程。我正在使用Xcode 4.3.3并且我检查了所有代码,它与教程中的代码相同(我确信这是正确的)。我一直在浏览网站,试图找到有同样问题的人,但我无法解决它,所以我感谢任何帮助。这是每当我尝试运行应用程序时出现的消息。

2012-07-23 14:22:01.021 Tip_Calculator[554:f803] -[ViewController initWithCoder:]: 无法识别的选择器发送到实例 0x68a3130 2012-07-23 14:22:01.025 Tip_Calculator[554:f803] * 由于应用程序终止未捕获的异常'NSInvalidArgumentException',原因是: ' - [视图控制器的initWithCoder:]:无法识别的选择发送到实例0x68a3130' *第一掷调用堆栈:(0x13c8022 0x1559cd6 0x13c9cbd 0x132eed0 0x132ecb2 0x234135 0x333c6e 0x333383 0x233cad 0x333c6e 0x33367b 0x333383 0x233105 0x43ceef 0x43d03e 0x11d7a 0x11ff8 0x1117f 0x20183 0x20c38 0x14634 0x12b2ef5 0x139c195 0x1300ff2 0x12ff8da 0x12fed84 0x12fec9b 0x10c65 0x12626 0x253d 0x24a5)终止调用抛出异常(lldb)

这是main.m中标记的区域:

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
@autoreleasepool {        return UIApplicationMain(argc, argv, nil,     NSStringFromClass([AppDelegate class]));    }
}

我检查了所有的接口连接,它们都很好。我还添加了一个异常断点,出现了这个错误:

2012-07-23 20:49:53.633 Tip_Calculator[2067:f803]-[ViewController initWithCoder:]: 无法识别的选择器发送到实例 0x68c09c0 (lldb)

这是显示的错误日志:

Tip_Calculator`start:
0x2470:  pushl  $0
0x2472:  movl   %esp, %ebp
0x2474:  andl   $-16, %esp
0x2477:  subl   $16, %esp
0x247a:  movl   4(%ebp), %ebx
0x247d:  movl   %ebx, (%esp)
0x2480:  leal   8(%ebp), %ecx
0x2483:  movl   %ecx, 4(%esp)
0x2487:  addl   $1, %ebx
0x248a:  shll   $2, %ebx
0x248d:  addl   %ecx, %ebx
0x248f:  movl   %ebx, 8(%esp)
0x2493:  movl   (%ebx), %eax
0x2495:  addl   $4, %ebx
0x2498:  testl  %eax, %eax
0x249a:  jne    0x00002493               ; start + 35
0x249c:  movl   %ebx, 12(%esp)
0x24a0:  calll  0x000024b0               ; main at main.m:14
**0x24a5:  movl   %eax, (%esp)**
0x24a8:  calll  0x000033ca               ; exit
0x24ad:  hlt    
0x24ae:  nop    
0x24af:  nop 

我在标记区域周围加上星号。无论是否存在异常断点,都会显示相同的错误。

我正在使用情节提要,这是ViewController.h的文件:

#import <UIKit/UIKit.h>

@interface ViewController : NSObject
{
//outlets
IBOutlet UITextField *billField;

IBOutlet UITextField *tipFieldTen;
IBOutlet UITextField *tipFieldFifteen;
IBOutlet UITextField *tipFieldTwenty;
IBOutlet UITextField *tipFieldCustom;
IBOutlet UITextField *totalFieldTen;
IBOutlet UITextField *totalFieldFifteen;
IBOutlet UITextField *totalFieldTwenty;
IBOutlet UITextField *totalFieldCustom;
IBOutlet UILabel *customPercentLabel;
IBOutlet UISlider *customPercentSlider;

NSString *billTotal;
}
-(IBAction)calculateTip:(id)sender;



@end

这是.m文件:

#import "ViewController.h"

@implementation ViewController

- (void)awakeFromNib; 
{
[billField becomeFirstResponder]; //display keyboard for billField
}

-(IBAction)calculateTip:(id)sender
{
static BOOL toggle = YES;

if (toggle)
{
    toggle = NO;
    NSString *billFieldText = billField.text;

    float newTotal = [billFieldText floatValue];
    float customTipPercent = customPercentSlider.value;

    if(sender ==billField)
    {
        if (billFieldText.length <billTotal.length)
            billTotal = [NSString stringWithFormat:@"%.02f", 
                         newTotal / 10];
        else
            billTotal = [NSString stringWithFormat:@"%.02f",
                         newTotal * 10];

        billField.text = billTotal;

        newTotal = [billTotal floatValue];

        float tenTip = newTotal * 0.10;
        float fifteenTip = newTotal * 0.15;
        float twentyTip = newTotal * 0.20;

        tipFieldTen.text = [NSString stringWithFormat:@"%.02f", tenTip];
        tipFieldFifteen.text = [NSString stringWithFormat:@"%.02f", fifteenTip];
        tipFieldTwenty.text = [NSString stringWithFormat:@"%.02f", twentyTip];

        totalFieldTen.text = [NSString stringWithFormat:@"%.02f", newTotal + tenTip];
        totalFieldFifteen.text = [NSString stringWithFormat:@"%.02f", newTotal +   fifteenTip];
        totalFieldTwenty.text = [NSString stringWithFormat:@"%.02f", newTotal + twentyTip];
    }
    else if (sender == customPercentSlider)
    {
        int percentage = (int)(customTipPercent * 100);
        customPercentLabel.text = [NSString stringWithFormat:@"%i%%", percentage];
        float newSliderValue = ((float) percentage) / 100;
        customPercentSlider.value = newSliderValue;
        customTipPercent = newSliderValue;
    }
    float customTip = customTipPercent * newTotal;
    tipFieldCustom.text = [NSString stringWithFormat:@"%.02f", customTip];
    totalFieldCustom.text = [NSString stringWithFormat:@"%.02f", customTip + newTotal];
}
else
{
    toggle = YES;
}

}



@end

任何帮助将不胜感激,因为我是Objective-c 的新手。

4

1 回答 1

3

你有

@interface ViewController : NSObject

这应该是:

@interface ViewController : UIViewController

原因:

所以你的ViewController类当前继承自NSObject,它没有实现initWithCoder。事实上,这个方法调用是NSCoding协议的一部分,它UIViewController采用。

背景:

您收到的错误基本上是说该类的对象ViewController被发送了一个选择器或“消息” initWithCoder,但它没有该方法的实现。因此,您通常会做两件事来追踪这一点:

1)检查您是否在代码中的任何地方发送该消息,然后检查接收它的对象是否属于应该识别该方法的类。您还必须考虑从 UIKit 等框架调用该方法,这使这变得更加困难。

2)如果上面看起来没问题,那么您必须考虑,也许当您的代码假定一个对象是一种类型时,它实际上可能是另一种类型,因此会发生此错误。

考虑这段代码:

NSArray *anArray = [[NSArray alloc] initWithObjects:@"aString", nil];
NSNumber *aNumber = [anArray objectAtIndex:0];
BOOL isEqual = [aNumber isEqualToNumber:[NSNumber numberWithInteger:42]];

这将在没有警告的情况下编译,但是在运行时会导致异常 - 因为它假定数组中的对象是NSNumber,但实际上它是NSString

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFConstantString isEqualToNumber:]:无法识别的选择器发送到实例 0x6cbfc”

于 2012-07-23T20:20:26.327 回答