错误:此类不支持键值编码的关键路径keyPath。

10
 UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"SettingsViewController"];
    [[self navigationController] pushViewController:vc animated:YES];

尝试在Storyboard中从我的NavigationController推出一个视图控制器,但我一直收到这个错误:

* Terminating app due to uncaught exception 'NSUnknownKeyException',reason:'[<SettingsViewController 0x1f98f100> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key keyPath.'* First throw call stack: (0x31f4a3e7 0x39c3b963 0x31f4a0d5 0x327b87d9 0x327b4543 0x31ed08a5 0x33ef5e69 0x340e5b09 0x71aa3 0x33e1b31d 0x33e9dda9 0x32859657 0x31f1f857 0x31f1f503 0x31f1e177 0x31e9123d 0x31e910c9 0x35a6f33b 0x33dad2b9 0x6567d 0x3a068b20)


相关链接:https://dev59.com/onA75IYBdhLWcg3w0sl9 - jtbandes
4个回答

45

我因为在“Identity inspector”选项卡中的“User Defined Runtime Attributes”字段里填写了一些内容而出现了这个错误。因为我的疏忽,我在“Key Path”列中留下了默认的keyPath值,导致了问题。

xcode界面构建器的截图


你需要更改关键路径(layer.circleRadius)、它的类型(number)和值(30)...例如,假设你需要将正方形转换为圆形,则layer.circleRadius,类型:number,值为(height/2或width/2)。 - Hardik Bar
我怀疑我永远不会找到这个问题,谢谢,确实是我的问题。 - James T Snell

4
很可能,在Storyboard中定义的SettingsViewController包含对名为keyPath的IBOutlet的引用,但是在类中未定义keyPath。
要解决此问题,请在Storyboard中选择视图控制器,选择连接检查器(右侧窗格的最右选项卡),并查看连接。可能有一个名为keyPath的连接,并且它可能附近有一个“!”。删除该连接(以及任何其他带有“!”的连接)。

1

Use this & check:

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 SettingsViewController *myVC = (SettingsViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SettingsViewController"];
[self.navigationController pushViewController:myVC animated:YES];

2
这是更好的代码,但不会解决问题。只要您不打算调用SettingsViewController上定义的任何方法,将vc声明为通用视图控制器是可以接受的。 - danh

0

右键点击XIB文件,以源代码形式打开文件,查找“keyPath”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接