Ios 目标C问题:由于未捕获异常而终止应用程序';NSUnknownKeyException';

Ios 目标C问题:由于未捕获异常而终止应用程序';NSUnknownKeyException';,ios,objective-c,Ios,Objective C,我最近开始尝试编写我的第一个iPhone应用程序,但在运行时遇到了一些问题,我不确定这里到底发生了什么。对不起,如果这是一个愚蠢的错误。主要是我的代码中没有任何错误或警告,所以我不明白为什么会发生这种情况。如果有人能帮忙,那就太好了 控制台输出: 2014-06-05 18:45:23.563 1RMCalculator[2239:60b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason:

我最近开始尝试编写我的第一个iPhone应用程序,但在运行时遇到了一些问题,我不确定这里到底发生了什么。对不起,如果这是一个愚蠢的错误。主要是我的代码中没有任何错误或警告,所以我不明白为什么会发生这种情况。如果有人能帮忙,那就太好了

控制台输出:

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

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

此问题很可能与interface builder有关。你有没有在任何
IBOutlets
上混来混去,可能留下一个孤儿?如果一切看起来都井然有序,那么也许你删除了一个,只需要做一个干净的构建。转到Xcode管理器>项目>“删除”衍生数据。然后执行一个干净的构建,看看这是否解决了问题。

还有什么需要检查的吗?在interface builder中的任何视图上是否设置了任何“用户定义的运行时属性”?(它们显示在每个视图的“标识检查器”中。如果这些指定的对象属性不存在,则会出现此错误


新手不太可能使用该功能,但可能您是在遵循一个教程,该教程向您展示了如何使用该功能。至少值得一提。

该错误看起来不像来自此代码。该错误表示您正在编写类似于
[someObject setValue:someforkey:@“reps”]
,而该对象是一个ViewController,而不是您认为它应该是什么。是的,我有两个孤立的插座。谢谢!
#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