尝试使用按钮设置NSUserDefault时应用程序崩溃

时间:2016-04-14 18:28:11

标签: ios swift uibutton nsuserdefaults

我正在创建一个应用程序并尝试从另一个视图控制器更新NSUserDefault,虽然我不认为它是单独的视图控制器,因为我无法在此控制器上设置新的NSUserDefault。每按一下按钮都会崩溃,继承视图控制器的代码:

//  Upgrades.swift
//  Unit3Final
//
//  Created by fgstu on 4/14/16.
//  Copyright © 2016 AllenH. All rights reserved.
//

    import UIKit

    class Upgrades: UIViewController {


    var tapModifier = (NSUserDefaults.standardUserDefaults().integerForKey("tapModifier"))


    func storeTheScore() {
        NSUserDefaults.standardUserDefaults().setInteger(tapModifier, forKey: "tapModifier")
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func BuyAHut(sender: UIButton) {

        tapModifier += 5

        storeTheScore()

    }



}

在我的viewDidLoad中使用

在我的主视图控制器中设置它
var tapModifier: Int = 1
NSUserDefaults.standardUserDefaults().setInteger(tapModifier, forKey: "tapModifier")

,错误消息是

2016-04-14 12:13:45.264 Unit3Final[40985:5120078] -[Unit3Final.Upgrades hutUpgrade:]: unrecognized selector sent to instance 0x7fadab4b2e80
2016-04-14 12:13:45.270 Unit3Final[40985:5120078] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Unit3Final.Upgrades hutUpgrade:]: unrecognized selector sent to instance 0x7fadab4b2e80'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010258de65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001042cddeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010259648d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x00000001024e390a ___forwarding___ + 970
    4   CoreFoundation                      0x00000001024e34b8 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x0000000102dac194 -[UIApplication sendAction:to:from:forEvent:] + 92
    6   UIKit                               0x0000000102f1b6fc -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x0000000102f1b9c8 -[UIControl _sendActionsForEvents:withEvent:] + 311
    8   UIKit                               0x0000000102f1aaf8 -[UIControl touchesEnded:withEvent:] + 601
    9   UIKit                               0x0000000102e1b49b -[UIWindow _sendTouchesForEvent:] + 835
    10  UIKit                               0x0000000102e1c1d0 -[UIWindow sendEvent:] + 865
    11  UIKit                               0x0000000102dcab66 -[UIApplication sendEvent:] + 263
    12  UIKit                               0x0000000102da4d97 _UIApplicationHandleEventQueue + 6844
    13  CoreFoundation                      0x00000001024b9a31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x00000001024af95c __CFRunLoopDoSources0 + 556
    15  CoreFoundation                      0x00000001024aee13 __CFRunLoopRun + 867
    16  CoreFoundation                      0x00000001024ae828 CFRunLoopRunSpecific + 488
    17  GraphicsServices                    0x0000000106bcdad2 GSEventRunModal + 161
    18  UIKit                               0x0000000102daa610 UIApplicationMain + 171
    19  Unit3Final                          0x000000010201db7d main + 109
    20  libdyld.dylib                       0x0000000104ddf92d start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1 个答案:

答案 0 :(得分:0)

它没有值,这就是崩溃的原因,最初尝试将其设置为0。

你正在添加5而不是这个,

tapModifier = NSUserDefaults etc...

试试......

tapModifier = 0