setValue:forUndefinedKey:此类不是密钥的密钥值编码兼容

时间:2012-12-09 23:50:22

标签: objective-c xcode cocoa-touch cocoa

我正在创建登录以查看支持服务单。第一个视图是故障单视图,如果您未登录,则会将您推送到登录屏幕。但是我收到了这个:

  

支持[3209:18e03] *由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是密钥LoginScreen的密钥值编码兼容。 *

     

第一次抛出调用堆栈:   (0x1f17012 0x1634e7e 0x1f9ffb1 0x10e1711 0x1062ec8 0x10629b7 0x108d428 0x7990cc 0x1648663 0x1f1245a 0x797bcf 0x65ce37 0x65d418 0x65d648 0x65d882 0x669235 0x8683d2 0x6664f3 0x666777 0x6667b7 0x9d1fe2 0x9c3ad9 0x65e422 0x2b15 0x65f753 0x65fb2f 0x661286 0x65fe3f 0x592910 0x592895 0x5926e5 0x1edfafe 0x1edfa3d 0x1ebd7c2 0x1ebcf44 0x1ebce1b 0x22aa7e3 0x22aa668 0x57c65c 0x1f0d 0x1e35)   libc ++ abi.dylib:terminate调用抛出异常

这是我的LoginScreen.m代码

#import "UIAlertView+error.h"
#import "LoginScreen.h"
#import "API.h"
#include <CommonCrypto/CommonDigest.h>

#define sayWhat @"REMOVEDFORSECURITY"


@implementation LoginScreen

-(void)viewDidLoad {
    NSLog(@"Found me!");
    [super viewDidLoad];


    //focus on the username field /show keyboard
    [fldUsername becomeFirstResponder];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    -(IBA

ction)btnLoginRegisterTapped:(UIButton*)sender {
    if (fldUsername.text.length < 4 || fldPassword.text.length < 4){
        [UIAlertView error:@"Enter username and password over 4 chars each."];
        return;
    }

    //very basic encryption called "salting"
    NSString* saltedPassword = [NSString stringWithFormat:@"%@%@", fldPassword.text, sayWhat];

    //prep the hashed storage
    NSString* hashedPassword = nil;
    unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];

    //actually hash this
    NSData *data = [saltedPassword dataUsingEncoding: NSUTF8StringEncoding];
    if (CC_SHA1([data bytes], [data length], hashedPasswordData)){
        hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
        } else {
            [UIAlertView error:@"Password can't be sent"];
                return;
        }

    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
        fldUsername.text, @"username",
                                   hashedPassword, @"password", nil];
    //here is the web call finally!
    [[API sharedInstance] commandWithParams:params
                               onCompletion:^(NSDictionary *json){
        //returned result
       NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];

        if([json objectForKey:@"error"]==nil && [[res objectForKey:@"userid"] intValue]> 0) {
                                       //success
        } else {
                                       //error'
        [UIAlertView error:[json objectForKey:@"error"]];
                                   }

    }];


}
@end

和LoginScreen.h

#import <UIKit/UIKit.h>

@interface LoginScreen : UIViewController {
//the login form fields
IBOutlet UITextField* fldUsername;
IBOutlet UITextField* fldPassword;

}

//action for when either button is pressed
-(IBAction)btnLoginRegisterTapped:(id)sender;

@end


    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="2844" systemVersion="12C60" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="A8c-eR-geg">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="1930"/>
    </dependencies>
    <scenes>
        <!--Tickets View - View Tickets-->
        <scene sceneID="aIJ-Wx-2Yy">
            <objects>
                <viewController title="View Tickets" id="T0P-Bi-31w" customClass="TicketsView" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="4zZ-I2-ltX">
                        <rect key="frame" x="0.0" y="64" width="320" height="504"/>
                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                        <subviews>
                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="You Made It" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Fbu-Sr-Mgf">
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                        <constraints>
                            <constraint firstItem="Fbu-Sr-Mgf" firstAttribute="top" secondItem="4zZ-I2-ltX" secondAttribute="top" constant="138" id="QBp-qM-fKP"/>
                            <constraint firstItem="Fbu-Sr-Mgf" firstAttribute="leading" secondItem="4zZ-I2-ltX" secondAttribute="leading" constant="115" id="goO-5h-RZK"/>
                        </constraints>
                    </view>
                    <navigationItem key="navigationItem" id="vu4-JX-C3v"/>
                    <simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/>
                    <connections>
                        <segue destination="TOB-FA-Ss4" kind="modal" identifier="ShowLogin" id="dJH-ge-8CV"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="aSa-2G-5fx" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="-518" y="-841"/>
        </scene>
        <!--Login Screen - Login-->
        <scene sceneID="6iZ-QL-7ID">
            <objects>
                <viewController title="Login" id="TOB-FA-Ss4" customClass="LoginScreen" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="w9e-4U-cwC">
                        <rect key="frame" x="0.0" y="64" width="320" height="504"/>
                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                        <subviews>
                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Blue Label Hosting Login" lineBreakMode="tailTruncation" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="SuD-JI-L2T">
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Username" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="NeI-zB-U8Y">
                                <constraints>
                                    <constraint firstAttribute="height" constant="31" id="BlH-cG-Jdh"/>
                                    <constraint firstAttribute="width" constant="208" id="Osb-tj-fjn"/>
                                </constraints>
                                <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                <textInputTraits key="textInputTraits"/>
                            </textField>
                            <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Password" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="4Fi-NX-kli">
                                <constraints>
                                    <constraint firstAttribute="height" constant="31" id="X3j-X2-ton"/>
                                </constraints>
                                <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                <textInputTraits key="textInputTraits" secureTextEntry="YES"/>
                            </textField>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="5o5-83-KbW">
                                <constraints>
                                    <constraint firstAttribute="height" constant="36" id="Afu-6h-uru"/>
                                    <constraint firstAttribute="width" constant="161" id="Ef5-df-Jmp"/>
                                </constraints>
                                <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
                                <state key="normal" title="Login">
                                    <color key="titleColor" red="0.19607843459999999" green="0.30980393290000002" blue="0.52156865600000002" alpha="1" colorSpace="calibratedRGB"/>
                                    <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
                                </state>
                                <state key="highlighted">
                                    <color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                                </state>
                                <connections>
                                    <action selector="btnLoginRegisterTapped:" destination="w9e-4U-cwC" eventType="touchUpInside" id="Wgb-L8-sAJ"/>
                                </connections>
                            </button>
                        </subviews>
                        <color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
                        <constraints>
                            <constraint firstItem="NeI-zB-U8Y" firstAttribute="top" secondItem="SuD-JI-L2T" secondAttribute="bottom" constant="8" symbolic="YES" type="default" id="0Pv-zM-WId"/>
                            <constraint firstItem="4Fi-NX-kli" firstAttribute="top" secondItem="NeI-zB-U8Y" secondAttribute="bottom" constant="8" symbolic="YES" type="default" id="2Sw-m8-LND"/>
                            <constraint firstItem="4Fi-NX-kli" firstAttribute="trailing" secondItem="NeI-zB-U8Y" secondAttribute="trailing" type="default" id="88O-VQ-qut"/>
                            <constraint firstItem="SuD-JI-L2T" firstAttribute="centerX" secondItem="NeI-zB-U8Y" secondAttribute="centerX" type="default" id="Qu9-PG-YWz"/>
                            <constraint firstItem="4Fi-NX-kli" firstAttribute="centerX" secondItem="5o5-83-KbW" secondAttribute="centerX" type="default" id="RAm-sI-Z3u"/>
                            <constraint firstItem="5o5-83-KbW" firstAttribute="top" secondItem="w9e-4U-cwC" secondAttribute="top" constant="128" id="UTG-NS-bx4"/>
                            <constraint firstItem="SuD-JI-L2T" firstAttribute="top" secondItem="w9e-4U-cwC" secondAttribute="top" constant="20" symbolic="YES" type="default" id="iaC-Ex-EBa"/>
                            <constraint firstItem="SuD-JI-L2T" firstAttribute="centerX" secondItem="w9e-4U-cwC" secondAttribute="centerX" type="default" id="oWP-Gp-Pi5"/>
                            <constraint firstItem="4Fi-NX-kli" firstAttribute="leading" secondItem="NeI-zB-U8Y" secondAttribute="leading" type="default" id="oxE-WD-fVc"/>
                        </constraints>
                        <connections>
                            <outlet property="fldPassword" destination="4Fi-NX-kli" id="4eb-YW-8YI"/>
                            <outlet property="fldUsername" destination="NeI-zB-U8Y" id="Ds4-XS-iqW"/>
                        </connections>
                    </view>
                    <simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="FL3-Yk-JbH" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="-84" y="-349"/>
        </scene>
        <!--Navigation Controller-->
        <scene sceneID="fJ6-wq-4Tj">
            <objects>
                <navigationController id="A8c-eR-geg" sceneMemberID="viewController">
                    <toolbarItems/>
                    <navigationBar key="navigationBar" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" id="bnk-dD-E4j">
                        <rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
                        <autoresizingMask key="autoresizingMask"/>
                    </navigationBar>
                    <nil name="viewControllers"/>
                    <connections>
                        <segue destination="T0P-Bi-31w" kind="relationship" relationship="rootViewController" id="3jA-WY-c3V"/>
                    </connections>
                </navigationController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="lTO-TW-owb" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="-971" y="-910"/>
        </scene>
    </scenes>
    <classes>
        <class className="LoginScreen" superclassName="UIViewController">
            <source key="sourceIdentifier" type="project" relativePath="./Classes/LoginScreen.h"/>
            <relationships>
                <relationship kind="action" name="btnLoginRegisterTapped:"/>
                <relationship kind="outlet" name="fldPassword" candidateClass="UITextField"/>
                <relationship kind="outlet" name="fldUsername" candidateClass="UITextField"/>
            </relationships>
        </class>
        <class className="NSLayoutConstraint" superclassName="NSObject">
            <source key="sourceIdentifier" type="project" relativePath="./Classes/NSLayoutConstraint.h"/>
        </class>
        <class className="TicketsView" superclassName="UIViewController">
            <source key="sourceIdentifier" type="project" relativePath="./Classes/TicketsView.h"/>
        </class>
    </classes>
    <simulatedMetricsContainer key="defaultSimulatedMetrics">
        <simulatedStatusBarMetrics key="statusBar"/>
        <simulatedOrientationMetrics key="orientation"/>
        <simulatedScreenMetrics key="destination" type="retina4"/>
    </simulatedMetricsContainer>
</document>

20 个答案:

答案 0 :(得分:261)

这种莫名其妙的异常通常是不洁xib文件的结果。 打开xcode中的xib,选择文件所有者,然后单击“连接检查器”(右上箭头),立即查看所有插座。 查找表示缺少插座的!

答案 1 :(得分:37)

在Interface Builder中检查您的连接。 您可能指的是不存在的IBOutletIBAction

答案 2 :(得分:32)

Look "!"

谢谢你们。我通过你的帮助解决了这个问题。所以,我希望这个截图对有相同问题的人有帮助。

答案 3 :(得分:24)

转到Xcode的断点选项卡。使用底部的按钮添加异常断点。现在,您将看到调用setValue:forKey:和相关堆栈的代码。幸运的是,这将直接指向问题的来源。

奇怪您的类是LoginScreen,但错误是说某人正在使用“LoginScreen”作为密钥。检查LoginScreen.m是否是目标的一部分。

enter image description here


脚注:对于Swift,如果您更改类的名称,则会出现一个常见问题(因此,您可以在代码中的任何位置重命名)。故事板与此斗争,你通常必须重新拖动涉及该类的任何连接。特别是,重新输入右侧IdentityInspector选项卡中使用的类的名称。 (在图片示例中,我故意拼错了类名。但是当你重命名一个类时经常发生同样的事情; 即使它在IdentityInspector中看似正确,你需要再次输入名称;它会正确自动完成,你很高兴。)

答案 4 :(得分:22)

我在创建自定义TableCell方面遇到了类似的问题。问题是我将FileOwner设置为我的自定义类,然后连接出口。

FileOwner应该仍然是NSObject。

答案 5 :(得分:7)

我有这个并查看了所有内容并且没有看到任何问题,但最终还记得尝试清理并清除派生数据并解决了它! / p>

答案 6 :(得分:2)

我遇到了类似的问题,但我使用initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil明确地使用类的名称作为传递的字符串(是的错误形式!)。

我最后使用稍微不同的名称删除并重新创建视图控制器,但忽略了更改方法中指定的字符串,因此我的旧版本仍在使用 - 即使它在垃圾箱中!

我可能会按照Is passing two nil paramters to initWithNibName:bundle: method bad practice (i.e. unsafe or slower)?

中的建议继续使用此结构
- (id)init
{
    [super initWithNibName:@"MyNib" bundle:nil];
    ... typical initialization ...
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    return [self init];
}

希望这有助于某人!

答案 7 :(得分:2)

这个错误是别的!

以下是我如何修复它。我正在使用xcode Version 6.1.1并使用swift。每当我的应用程序尝试执行segue跳转到下一个屏幕时,我都会收到此错误。这就是我做的。

  1. 检查按钮是否连接到正确的操作。(这不是问题,但仍然很好检查)
  2. 检查按钮是否没有您可能错误创建的任何其他操作或插座。 (这不是问题,但仍然很好检查)
  3. 检查日志并确保NEXT SCREEN中的所有按钮都有正确的操作,如果有任何segues,请确保它们具有唯一标识符。 (这是问题)
    • 其中一个segue没有唯一标识符
    • 其中一个按钮有一个动作和两个我错误创建的插座。
  4. 删除所有其他插座,并确保您到下一个屏幕的segues具有唯一标识符。

    干杯,

答案 8 :(得分:1)

我遇到了这个问题,因为我没有使用StoryBorad,而且还有 项目属性 - &gt;部署信息 - &gt;主界面是主Xib的名称。

我删除了主界面中的值并解决了问题。

答案 9 :(得分:0)

我在所有地方的PickerView上都有这个。错误消息是在消息的第一个位置使用“queuePicker”命名问题。它是由一个先前的Referencing Outlet引起的,在我添加一个组件并重命名除了queuePicker之外的原始NSArray之后,它不再有效。 CNTL +点击你的StoryBoard对象为你提供连接。我在Referencing Outlets中点击了ViewController旁边的“x”for queuePicker,建立了新的连接,并且中提琴不再崩溃。

答案 10 :(得分:0)

您可能在alertView中为某个键设置了一个值,这是不允许的。在这种情况下,关键是LoginScreen。我没有看到对setValue()的任何调用,所以我认为它在代码中的其他位置。

答案 11 :(得分:0)

我今天遇到了同样的问题。正如建议in this answer,问题是一个不洁净的xib。在我的情况下,不洁的xib是更新xib的结果,该xib是由与其关联的视图控制器以外的其他东西加载的。

Xcode让我创建并填充新的插座并将其连接到文件的所有者,即使我明确地将其连接到正确的视图控制器的源。这是Xcode生成的代码:

    <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="LoginViewController"]]>
        <connections>
            <outlet property="hostLabel" destination="W4x-T2-Mcm" id="c3E-1U-sVf"/>
        </connections>
    </placeholder>

当我运行我的应用时,它崩溃时出现相同的not key value coding-compliant错误。为了解决这个问题,我从Interface Builder中的文件所有者中删除了插座,并将其显式连接到左侧轮廓上的视图控制器对象,而不是助手编辑器中的代码。

答案 12 :(得分:0)

在以下情况中发生了这种情况:

我在工作区中创建了第二个项目,为模板选择了“单一视图应用程序”。

然后我转到Interface Builder(主要的iPhone故事板),在主视图中添加了UISwitch,并通过IBOutletIBAction将其连接到视图控制器( -valueChanged:)。

启动时,应用程序崩溃,问题中提到的异常。如果我从视图中删除开关,它就可以工作。

仔细检查后,我意识到我控制了将连接拖到同一工作区中另一个项目的ViewController.h。 “自动”界面生成器的助手编辑器(a.k.a Tuxedo胸部图标)选择了“对应”错误文件(使用正确的名称)。

希望这对某人有所帮助,特别是因为“单一视图应用程序”和“SpriteKit游戏”项目模板(在我的情况下)都创建了一个名为“ViewController”的默认视图控制器类。

答案 13 :(得分:0)

如果您有不同的storybord文件,并且如果您的头文件中有出口创建的出口参考,那么您只需右键单击文件所有者即可删除连接。

文件所有者 - &gt;右击 - &gt;删除不需要的连接。

仔细阅读此内容。 What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"

答案 14 :(得分:0)

这通常意味着有些东西试图访问 @property&#34; givenName&#34;

如果您使用Interface Builder(IB)做某事,通常的原因是您:

  1. 从课程中删除了该属性,但未删除该属性 IB中的连接
  2. OR:你有一个File的所有者对象设置为错误的类(检查 属性 - 取决于您使用的xcode版本
    • 找到其设置为的类名。您可能复制/粘贴了NIB文件,并且没有在NIB中更改此字段),并且您已经连接起来了 该课程的出口,但您实际的文件所有者是一个东西 不同

答案 15 :(得分:0)

在我的情况下,我在.h中有 IBOutlet UILabel *描述,它是黄色/!\ - “将不会合成”,我记得。不知道它是什么,为什么只有这个标签。

但是我得到了这个崩溃和上面的错误。删除*描述并重新创建* description2。结果没有崩溃。

答案 16 :(得分:0)

对我来说,我为我的集合视图标题切换了一个调用,在那里我注册了nib来注册该类。修好了。

答案 17 :(得分:0)

因为没有2个功能

@implementation CellTableView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    return [self init];
}
- (void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end

答案 18 :(得分:0)

我在创建自定义视图类时遇到了类似的错误,这是因为某个插座在XIB文件中被连接了两次(我想我最初控制将控件直接拖到代码中,但后面的控件被拖了再次来自File的所有者)。我打开了XIB文件并删除其中一个,之后一切正常。希望这会有所帮助。

答案 19 :(得分:-1)

我必须删除所有对象并重新添加它们。这似乎解决了这个问题。

相关问题