0

我是所有编程的初学者,并且一直在尝试从 Big Nerd Ranch Books 中实现一些自学的东西。但是我真的被这个问题难住了,是的,我已经在这个论坛和其他论坛中搜索了可能的解决方案,但没有成功。这是代码:

ANKYViewController.h:

#import <UIKit/UIKit.h>

@interface ANKYViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *weightFieldDeadlift;
@property (weak, nonatomic) IBOutlet UITextField *repFieldDeadlift;
@property (strong, nonatomic) IBOutlet UILabel *workingOneRMDeadlift;

@property (weak, nonatomic) IBOutlet UITextField *weightFieldBenchPress;
@property (weak, nonatomic) IBOutlet UITextField *repFieldBenchPress;
@property (strong, nonatomic) IBOutlet UILabel *workingOneRMBenchPress;

@property (weak, nonatomic) IBOutlet UITextField *weightFieldSquat;
@property (weak, nonatomic) IBOutlet UITextField *repFieldSquat;
@property (strong, nonatomic) IBOutlet UILabel *workingOneRMSquat;

@property (weak, nonatomic) IBOutlet UITextField *weightFieldMilitaryPress;
@property (weak, nonatomic) IBOutlet UITextField *repFieldMilitaryPress;
@property (strong, nonatomic) IBOutlet UILabel *workingOneRMMilitaryPress;

- (IBAction)calculateOneRM:(id)sender;

@end

ANKYViewController.m:

#import "ANKYViewController.h"

@interface ANKYViewController ()

@end

@implementation ANKYViewController
@synthesize weightFieldDeadlift;
@synthesize repFieldBenchPress;
@synthesize workingOneRMBenchPress;
@synthesize weightFieldSquat;
@synthesize repFieldSquat;
@synthesize workingOneRMSquat;
@synthesize weightFieldMilitaryPress;
@synthesize repFieldMilitaryPress;
@synthesize workingOneRMMilitaryPress;
@synthesize repFieldDeadlift;
@synthesize workingOneRMDeadlift;
@synthesize weightFieldBenchPress;

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

- (void)viewDidUnload
{
    [self setWeightFieldDeadlift:nil];
    [self setRepFieldDeadlift:nil];
    [self setWorkingOneRMDeadlift:nil];
    [self setWeightFieldDeadlift:nil];
    [self setRepFieldBenchPress:nil];
    [self setWeightFieldBenchPress:nil];
    [self setWorkingOneRMBenchPress:nil];
    [self setWeightFieldSquat:nil];
    [self setRepFieldSquat:nil];
    [self setWorkingOneRMSquat:nil];
    [self setWeightFieldMilitaryPress:nil];
    [self setRepFieldMilitaryPress:nil];
    [self setWorkingOneRMMilitaryPress:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)calculateOneRM:(id)sender {
    float dw = [[weightFieldDeadlift text]floatValue];
    float dr = [[repFieldDeadlift text]floatValue];
    float d = (dw * dr * 0.0333) + dw;
    NSLog(@"Deadlift: %f", d);
    NSString *deadlift = [[NSString alloc]initWithFormat:@"%f", d];
    [workingOneRMDeadlift setText:deadlift];

    float bpw = [[weightFieldBenchPress text]floatValue];
    float bpr = [[repFieldBenchPress text]floatValue];
    float bp = (bpw * bpr * 0.0333) + bpw;
    NSLog(@"Bench Press: %f", bp);
    NSString *benchPress = [[NSString alloc]initWithFormat:@"%f", bp];
    [workingOneRMBenchPress setText:benchPress];

    float sw = [[weightFieldSquat text]floatValue];
    float sr = [[repFieldSquat text]floatValue];
    float s = (sw * sr * 0.0333) + sw;
    NSLog(@"Squat: %f", s);
    NSString *squat = [[NSString alloc]initWithFormat:@"%f", s];
    [workingOneRMSquat setText:squat];

    float mpw = [[weightFieldMilitaryPress text]floatValue];
    float mpr = [[repFieldMilitaryPress text]floatValue];
    float mp = (mpw * mpr * 0.0333) + mpw;
    NSLog(@"Military Press: %f", mp);
    NSString *militaryPress = [[NSString alloc]initWithFormat:@"%f", mp];
    [workingOneRMMilitaryPress setText:militaryPress];
}
@end

文件的所有者类已经被声明为 ANKYViewController。链接插座等是通过控制拖动而不是手动编码(我花了太多时间后放弃了)。

错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIApplication 0x6c22e70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key repsField.'

没有代码的复制和粘贴,而且任何地方都没有提到“repsField”(而不是repFieldxxx),这当然令人沮丧。

我希望这是足够的信息来帮助找到解决方案,因为我花了几天时间查看其他人的解决方案但没有成功。

谢谢。

4

2 回答 2

4

看起来问题是有人正在使用不符合 KVCUIApplication的密钥 ( ) 访问实例。repsField

更新我认为你甚至不需要子类来添加断点。转到断点导航器并为符号添加一个符号断点-[UIApplication setValue:forUndefinedKey:],然后运行应用程序。

我认为您可以通过子类化来调试它,UIApplication并在方法的虚拟覆盖中设置断点setValue:forUndefinedKey:

在您调用的文件中UIApplicationMain添加此类:

@interface TestApplication : UIApplication
@end
@implementation TestApplication
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    [super setValue:value forUndefinedKey:key]; // set breakpoint here
}
@end

然后将第三个参数更改UIApplicationMain@"TestApplication",例如:

UIApplicationMain(argc, argv, @"TestApplication", NSStringFromClass([AppDelegate class]));

现在运行应用程序,它应该会进入调试器,您应该能够看到导致它的调用堆栈。

于 2012-06-30T16:41:05.290 回答
1

当出现此错误消息时,通常是 xib 文件或情节提要中的连接错误。我猜这是一个旧示例,其中有一个 MainWindow.xib 文件,文件的所有者设置为 UIApplication 类。该 xib 中很可能存在repsField与 File's Owner 中调用的插座的连接,当然,该插座与实际类中的任何内容都不匹配。

于 2012-06-30T17:03:17.257 回答