IOS - NSUnknownKeyException

时间:2013-12-04 20:33:07

标签: ios objective-c

我正在努力开发IOS应用程序。我按照本指南的要求确定了

http://www.appcoda.com/how-to-get-current-location-iphone-user/

但是在主要方法中我得到以下错误;

  

2013-12-04 20:28:55.591 MyLocationDemo [20391:c07] *终止应用   由于未捕获的异常'NSUnknownKeyException',原因:   '[setValue:forUndefinedKey:]:   对于密钥getLocation,这个类不符合密码值编码。   * 第一次抛出调用堆栈:(0x1850012 0x116ce7e 0x18d8fb1 0xc18e41 0xb9a5f8 0xb9a0e7 0xbc4b58 0x2ce019 0x1180663 0x184b45a 0x2ccb1c   0x1917e7 0x191dc8 0x191ff8 0x192232 0xe13d5 0xe176f 0xe1905 0xea917   0xae96c 0xaf94b 0xc0cb5 0xc1beb 0xb3698 0x17abdf9 0x17abad0 0x17c5bf5   0x17c5962 0x17f6bb6 0x17f5f44 0x17f5e1b 0xaf17a 0xb0ffc 0x204d 0x1f75)   libc ++ abi.dylib:terminate调用抛出异常

有没有人知道我的代码可能有什么问题? 我修改过的文件是

//
//  main.m
//  MyLocationDemo
//

#import <UIKit/UIKit.h>

#import "MyLocationAppDelegate.h"
#import "MyLocationViewController.h"
int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([MyLocationAppDelegate class]));
    }
}


//
//  MyLocationViewController.h
//  MyLocationDemo
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>



@interface MyLocationViewController : UIViewController<CLLocationManagerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
- (IBAction)getLocation:(id)sender;

@end

这就是主要逻辑似乎

的地方
//
//  MyLocationViewController.m
//  MyLocationDemo
//
//  Created by Ricki Lambert on 04/12/2013.
//  Copyright (c) 2013 Ricki Lambert. All rights reserved.
//

#import "MyLocationViewController.h"



@interface MyLocationViewController ()

@end

@implementation MyLocationViewController

CLLocationManager *locationManager;


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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)getLocation:(id)sender {
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        _longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
}
@end

编辑:

添加了以下代码

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12F37" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="2">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="2083"/>
    </dependencies>
    <scenes>
        <!--My Location View Controller-->
        <scene sceneID="5">
            <objects>
                <viewController id="2" customClass="MyLocationViewController" sceneMemberID="viewController">
                    <view key="view" contentMode="scaleToFill" id="3">
                        <rect key="frame" x="0.0" y="20" width="320" height="548"/>
                        <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                        <subviews>
                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Lattitude" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="2NQ-NG-XNs">
                                <constraints>
                                    <constraint firstAttribute="width" constant="114" id="3lT-6l-45k"/>
                                    <constraint firstAttribute="height" constant="34" id="NHO-Yt-dF0"/>
                                </constraints>
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Longiude" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="RLl-n5-tOv">
                                <constraints>
                                    <constraint firstAttribute="width" constant="75" id="6AR-qh-Gnd"/>
                                    <constraint firstAttribute="height" constant="27" id="m84-iE-ZY7"/>
                                </constraints>
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Address" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1kJ-EE-aHk">
                                <constraints>
                                    <constraint firstAttribute="height" constant="27" id="6U9-sr-UXJ"/>
                                </constraints>
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Lattitude goes here" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fNS-Dr-1Vu">
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Longitude goes here" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="NoA-nn-nJE">
                                <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" text="Address goes here" lineBreakMode="tailTruncation" numberOfLines="4" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" preferredMaxLayoutWidth="158" translatesAutoresizingMaskIntoConstraints="NO" id="FRc-NM-7R7">
                                <constraints>
                                    <constraint firstAttribute="height" constant="110" id="Mxn-SQ-dDB"/>
                                    <constraint firstAttribute="width" constant="158" id="giI-DM-8Y6"/>
                                </constraints>
                                <fontDescription key="fontDescription" type="system" pointSize="14"/>
                                <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                <nil key="highlightedColor"/>
                            </label>
                            <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="tGJ-fU-7T9">
                                <constraints>
                                    <constraint firstAttribute="width" constant="194" id="wCV-5v-wAp"/>
                                </constraints>
                                <fontDescription key="fontDescription" type="boldSystem" pointSize="15"/>
                                <state key="normal" title="Get Address">
                                    <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="getLocation:" destination="2" eventType="touchUpInside" id="4Gy-OZ-lTb"/>
                                </connections>
                            </button>
                        </subviews>
                        <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
                        <constraints>
                            <constraint firstAttribute="trailing" secondItem="FRc-NM-7R7" secondAttribute="trailing" constant="20" symbolic="YES" type="default" id="0Hn-nk-jah"/>
                            <constraint firstItem="FRc-NM-7R7" firstAttribute="top" secondItem="3" secondAttribute="top" constant="177" id="2a9-5q-b4n"/>
                            <constraint firstItem="NoA-nn-nJE" firstAttribute="top" secondItem="RLl-n5-tOv" secondAttribute="top" type="default" id="8xW-ST-2rJ"/>
                            <constraint firstItem="RLl-n5-tOv" firstAttribute="leading" secondItem="2NQ-NG-XNs" secondAttribute="leading" type="default" id="9D0-0e-Dab"/>
                            <constraint firstItem="1kJ-EE-aHk" firstAttribute="top" secondItem="3" secondAttribute="top" constant="174" id="Atx-uI-F5b"/>
                            <constraint firstAttribute="bottom" secondItem="tGJ-fU-7T9" secondAttribute="bottom" constant="131" id="GmZ-gM-2LB"/>
                            <constraint firstItem="1kJ-EE-aHk" firstAttribute="leading" secondItem="RLl-n5-tOv" secondAttribute="leading" type="default" id="Gyh-cL-XiN"/>
                            <constraint firstItem="tGJ-fU-7T9" firstAttribute="centerX" secondItem="3" secondAttribute="centerX" type="default" id="Tb3-0Y-di7"/>
                            <constraint firstItem="FRc-NM-7R7" firstAttribute="leading" secondItem="NoA-nn-nJE" secondAttribute="leading" type="default" id="U11-d2-DSe"/>
                            <constraint firstItem="RLl-n5-tOv" firstAttribute="top" secondItem="3" secondAttribute="top" constant="104" id="VdS-si-Rf1"/>
                            <constraint firstItem="fNS-Dr-1Vu" firstAttribute="top" secondItem="3" secondAttribute="top" constant="43" id="cM5-dt-7bh"/>
                            <constraint firstItem="2NQ-NG-XNs" firstAttribute="top" secondItem="3" secondAttribute="top" constant="37" id="mV7-Ff-Uuy"/>
                            <constraint firstItem="2NQ-NG-XNs" firstAttribute="leading" secondItem="3" secondAttribute="leading" constant="39" id="spa-RY-ZpM"/>
                            <constraint firstItem="1kJ-EE-aHk" firstAttribute="trailing" secondItem="RLl-n5-tOv" secondAttribute="trailing" type="default" id="x03-z0-I5T"/>
                            <constraint firstItem="NoA-nn-nJE" firstAttribute="leading" secondItem="fNS-Dr-1Vu" secondAttribute="leading" type="default" id="x1R-k4-2ns"/>
                        </constraints>
                    </view>
                    <connections>
                        <outlet property="addressLabel" destination="FRc-NM-7R7" id="oNw-qt-0yh"/>
                        <outlet property="getLocation" destination="tGJ-fU-7T9" id="69B-gi-HDF"/>
                        <outlet property="latitudeLabel" destination="fNS-Dr-1Vu" id="hTv-w4-7tN"/>
                        <outlet property="longitudeLabel" destination="NoA-nn-nJE" id="OxD-hl-qRK"/>
                    </connections>
                </viewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="4" sceneMemberID="firstResponder"/>
            </objects>
        </scene>
    </scenes>
    <classes>
        <class className="MyLocationViewController" superclassName="UIViewController">
            <source key="sourceIdentifier" type="project" relativePath="./Classes/MyLocationViewController.h"/>
            <relationships>
                <relationship kind="action" name="getLocation:"/>
                <relationship kind="outlet" name="addressLabel" candidateClass="UILabel"/>
                <relationship kind="outlet" name="getLocation" candidateClass="UIButton"/>
                <relationship kind="outlet" name="latitudeLabel" candidateClass="UILabel"/>
                <relationship kind="outlet" name="longitudeLabel" candidateClass="UILabel"/>
            </relationships>
        </class>
        <class className="NSLayoutConstraint" superclassName="NSObject">
            <source key="sourceIdentifier" type="project" relativePath="./Classes/NSLayoutConstraint.h"/>
        </class>
    </classes>
    <simulatedMetricsContainer key="defaultSimulatedMetrics">
        <simulatedStatusBarMetrics key="statusBar"/>
        <simulatedOrientationMetrics key="orientation"/>
        <simulatedScreenMetrics key="destination" type="retina4"/>
    </simulatedMetricsContainer>
</document>

2 个答案:

答案 0 :(得分:0)

<relationship kind="outlet" name="getLocation" candidateClass="UIButton"/>

这会导致您的错误,您创建的getLocation插座不会指向ViewController中的任何内容。

解决此问题:

  • 转到Utilies窗格(右侧窗格)
  • 选择连接检查(箭头 - >;)
  • 删除对getLocation outlet
  • 的引用

答案 1 :(得分:0)

在您的故事板中执行以下操作

  1. 选择控制器
  2. 显示右侧面板
  3. 点击右侧最后一个连接检查器
  4. 检查所有连接是否有人有感叹号:然后将其删除
  5. enter image description here