ios - Objective-C 问题 : Terminating app due to uncaught exception 'NSUnknownKeyException'

标签 ios objective-c

<分区>

我最近开始尝试编写我的第一个 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

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 计算器。我认为这对于起点来说会很容易。该页面有两个文本字段和八个按钮。

最佳答案

这个问题很可能与界面生成器有关。你有没有在任何 IBOutlets 周围洗牌并且可​​能留下一个孤儿?如果一切看起来都井井有条,那么也许您删除了一个并且只需要进行干净的构建。转到 Xcode Organizer > 项目 >“删除”派生数据。然后执行干净的构建,看看是否能解决问题。

关于ios - Objective-C 问题 : Terminating app due to uncaught exception 'NSUnknownKeyException' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24071603/

上一篇:ios - 在 Objective-C 中解析 json

下一篇:ios - 在不引用文本字段的情况下关闭键盘

相关文章:

objective-c - iOS:UIPopoverController 内的人物选择器,搜索键盘扭曲弹出框

ios - 什么定义了 UIScrollView 分页大小?

objective-c - 在UIPickerView选择数组之前,按字母顺序对数组进行排序

ios - 将 View 转换为模态 Controller 的 ScrollView

ios - swift3 错误 : Argument labels '(configuration:, delegate:, delegateQueue:)' do not match any available overloads

ios - 如何为纯文本实现在应用程序中打开

ios - 为什么 API 拒绝我的 JSON?

ios - 如何使用 Objective-C iOS 读取 excel 文件?

iphone - 如何将 UIView 始终放在顶部屏幕上?

ios - 在已发布/可用的 iOS 应用程序的苹果商店中更改应用程序名称