Objective-C问题:由于未捕获的异常终止应用程序' NSUnknownKeyException'

时间:2014-06-05 23:02:17

标签: ios objective-c

我最近开始尝试编写我的第一个iPhone应用程序,我遇到了一些运行它的问题,我不确定这里发生了什么。对不起,如果这是一个愚蠢的noob错误。主要是我的代码中没有任何错误或警告,所以我很困惑为什么会这样。如果有人能提供帮助那就太棒了!

控制台输出:

2014-06-05 18:45:23.563 1RMCalculator[2239:60b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x1095242c0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key reps.'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010194a495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001016a999e objc_exception_throw + 43
    2   CoreFoundation                      0x00000001019ce919 -[NSException raise] + 9
    3   Foundation                          0x000000010128a530 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
    4   CoreFoundation                      0x0000000101946400 -[NSArray makeObjectsPerformSelector:] + 224
    5   UIKit                               0x00000001004b18a6 -[UINib instantiateWithOwner:options:] + 1131
    6   UIKit                               0x000000010034bb0c -[UIViewController _loadViewFromNibNamed:bundle:] + 245
    7   UIKit                               0x000000010034c149 -[UIViewController loadView] + 112
    8   UIKit                               0x000000010034c3b7 -[UIViewController loadViewIfRequired] + 75
    9   UIKit                               0x000000010034c777 -[UIViewController view] + 29
    10  UIKit                               0x000000010028c96b -[UIWindow addRootViewControllerViewIfPossible] + 58
    11  UIKit                               0x000000010028cc70 -[UIWindow _setHidden:forced:] + 282
    12  UIKit                               0x0000000100295ffa -[UIWindow makeKeyAndVisible] + 51
    13  UIKit                               0x0000000100251c98 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1788
    14  UIKit                               0x0000000100255a0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
    15  UIKit                               0x0000000100266d4c -[UIApplication handleEvent:withNewEvent:] + 3189
    16  UIKit                               0x0000000100267216 -[UIApplication sendEvent:] + 79
    17  UIKit                               0x0000000100257086 _UIApplicationHandleEvent + 578
    18  GraphicsServices                    0x0000000103ac371a _PurpleEventCallback + 762
    19  GraphicsServices                    0x0000000103ac31e1 PurpleEventCallback + 35
    20  CoreFoundation                      0x00000001018cc679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    21  CoreFoundation                      0x00000001018cc44e __CFRunLoopDoSource1 + 478
    22  CoreFoundation                      0x00000001018f5903 __CFRunLoopRun + 1939
    23  CoreFoundation                      0x00000001018f4d83 CFRunLoopRunSpecific + 467
    24  UIKit                               0x00000001002552e1 -[UIApplication _run] + 609
    25  UIKit                               0x0000000100256e33 UIApplicationMain + 1010
    26  1RMCalculator                       0x0000000100002883 main + 115
    27  libdyld.dylib                       0x0000000101fe25fd start + 1
    28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) IBOutlet UITextField *weightField;
@property (strong, nonatomic) IBOutlet UITextField *repsField;
@property (weak, nonatomic) IBOutlet UIButton *nintyFiveResult;
@property (weak, nonatomic) IBOutlet UIButton *nintyResult;
@property (weak, nonatomic) IBOutlet UIButton *eightyFiveResult;
@property (weak, nonatomic) IBOutlet UIButton *eightyResult;
@property (weak, nonatomic) IBOutlet UIButton *seventyFiveResult;
@property (weak, nonatomic) IBOutlet UIButton *seventyResult;
@property (weak, nonatomic) IBOutlet UIButton *sixtyFiveResult;
@property (weak, nonatomic) IBOutlet UIButton *sixtyResult;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

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

- (IBAction)weightModified:(UITextField *)sender {

    if([self.weightField.text isEqualToString:(@"0")] == false && [self.repsField.text isEqualToString:(@"0")] == false){

        // Gather reps and weight value
        int reps = 0;
        int weight = 0;
        weight = [sender.text intValue];
        reps = [self.repsField.text intValue];

        int nintyFiveInt = [self oneRepMax :0.95 :reps :weight];
        int nintyInt = [self oneRepMax :0.9 :reps :weight];
        int eightyFiveInt = [self oneRepMax :0.85 :reps :weight];
        int eightyInt = [self oneRepMax :0.8 :reps :weight];
        int seventyFiveInt = [self oneRepMax :0.75 :reps :weight];
        int seventyInt = [self oneRepMax :0.7 :reps :weight];
        int sixtyFiveInt = [self oneRepMax :0.65 :reps :weight];
        int sixtyInt = [self oneRepMax :0.6 :reps :weight];

        NSString *nintyFiveWeight = [NSString stringWithFormat:@"95%%: %d", nintyFiveInt];
        NSString *nintyWeight = [NSString stringWithFormat:@"95%%: %d", nintyInt];
        NSString *eightyFiveWeight = [NSString stringWithFormat:@"95%%: %d", eightyFiveInt];
        NSString *eightyWeight = [NSString stringWithFormat:@"95%%: %d", eightyInt];
        NSString *seventyFiveWeight = [NSString stringWithFormat:@"95%%: %d", seventyFiveInt];
        NSString *seventyWeight = [NSString stringWithFormat:@"95%%: %d", seventyInt];
        NSString *sixtyFiveWeight = [NSString stringWithFormat:@"95%%: %d", sixtyFiveInt];
        NSString *sixtyWeight = [NSString stringWithFormat:@"95%%: %d", sixtyInt];

        [self.nintyFiveResult setTitle:nintyFiveWeight forState:UIControlStateNormal];
        [self.nintyResult setTitle:nintyWeight forState:UIControlStateNormal];
        [self.eightyFiveResult setTitle:eightyFiveWeight forState:UIControlStateNormal];
        [self.eightyResult setTitle:eightyWeight forState:UIControlStateNormal];
        [self.seventyFiveResult setTitle:seventyFiveWeight forState:UIControlStateNormal];
        [self.seventyResult setTitle:seventyWeight forState:UIControlStateNormal];
        [self.sixtyFiveResult setTitle:sixtyFiveWeight forState:UIControlStateNormal];
        [self.sixtyResult setTitle:sixtyWeight forState:UIControlStateNormal];
    }
}

- (IBAction)repsModified:(UITextField *)sender {

    if([self.weightField.text isEqualToString:(@"0")] == false && [self.repsField.text isEqualToString:(@"0")] == false){

        // Gather reps and weight value
        int reps = 0;
        int weight = 0;
        weight = [sender.text intValue];
        reps = [self.repsField.text intValue];

        int nintyFiveInt = [self oneRepMax :0.95 :reps :weight];
        int nintyInt = [self oneRepMax :0.9 :reps :weight];
        int eightyFiveInt = [self oneRepMax :0.85 :reps :weight];
        int eightyInt = [self oneRepMax :0.8 :reps :weight];
        int seventyFiveInt = [self oneRepMax :0.75 :reps :weight];
        int seventyInt = [self oneRepMax :0.7 :reps :weight];
        int sixtyFiveInt = [self oneRepMax :0.65 :reps :weight];
        int sixtyInt = [self oneRepMax :0.6 :reps :weight];

        NSString *nintyFiveWeight = [NSString stringWithFormat:@"95%%: %d", nintyFiveInt];
        NSString *nintyWeight = [NSString stringWithFormat:@"95%%: %d", nintyInt];
        NSString *eightyFiveWeight = [NSString stringWithFormat:@"95%%: %d", eightyFiveInt];
        NSString *eightyWeight = [NSString stringWithFormat:@"95%%: %d", eightyInt];
        NSString *seventyFiveWeight = [NSString stringWithFormat:@"95%%: %d", seventyFiveInt];
        NSString *seventyWeight = [NSString stringWithFormat:@"95%%: %d", seventyInt];
        NSString *sixtyFiveWeight = [NSString stringWithFormat:@"95%%: %d", sixtyFiveInt];
        NSString *sixtyWeight = [NSString stringWithFormat:@"95%%: %d", sixtyInt];

        [self.nintyFiveResult setTitle:nintyFiveWeight forState:UIControlStateNormal];
        [self.nintyResult setTitle:nintyWeight forState:UIControlStateNormal];
        [self.eightyFiveResult setTitle:eightyFiveWeight forState:UIControlStateNormal];
        [self.eightyResult setTitle:eightyWeight forState:UIControlStateNormal];
        [self.seventyFiveResult setTitle:seventyFiveWeight forState:UIControlStateNormal];
        [self.seventyResult setTitle:seventyWeight forState:UIControlStateNormal];
        [self.sixtyFiveResult setTitle:sixtyFiveWeight forState:UIControlStateNormal];
        [self.sixtyResult setTitle:sixtyWeight forState:UIControlStateNormal];
    }
}

- (int)oneRepMax:(int) percent :(int) numReps :(int) weightToLift{

    return percent*(weightToLift*(numReps/30));
}

@end

如果好奇我正在创建一个基本的1RM计算器。我认为这对于一个起点来说很容易。该页面有两个文本字段和八个按钮。

2 个答案:

答案 0 :(得分:2)

此问题很可能与界面构建器有关。你是否在任何IBOutlets周围洗牌并且可​​能留下一个孤儿?如果一切都按顺序查看,那么也许你删除了一个,只需要做一个干净的构建。转到Xcode管理器&gt;项目&gt; &#34;删除&#34;派生数据。然后执行一个干净的构建,看看是否能解决问题。

答案 1 :(得分:0)

要检查的另一件事是什么?您是否在界面构建器中的任何视图上设置了“用户定义的运行时属性”? (它们显示在每个视图的“Identity Inspector”中。如果这些视图指定了不存在的对象属性,则会出现此错误。

新手不太可能使用该功能,但也许您正在按照教程向您展示如何使用该功能。这至少值得一提。