简体   繁体   English

Swift:“无法识别的选择器发送到实例”

[英]Swift: ¨Unrecognized selector sent to instance¨

Working on a countdown when i get this error when trying to run this code:尝试运行此代码时出现此错误时正在倒计时:

error code: Unrecognized selector sent to instance错误代码:无法识别的选择器发送到实例

class ViewController: UIViewController, UITableViewDelegate {

@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var label: UILabel!

weak var timer: NSTimer?

let formatter: NSDateComponentsFormatter = {
    let _formatter = NSDateComponentsFormatter()
    _formatter.allowedUnits = [.Year, .Month, .Day, .Hour]
    _formatter.unitsStyle = .Full

    return _formatter
}()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    datePicker.date = NSDate().dateByAddingTimeInterval(1000) // initialize it to whatever you want
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "didFireTimer:", userInfo: nil, repeats: true)
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)

    timer?.invalidate()
}

func didFireTimer(timer: NSTimer) {
    label.text = formatter.stringFromDate(NSDate(), toDate: datePicker.date)


}

The whole error message:整个错误信息:

2015-11-23 22:34:45.148 DatePicker xxTestxx[19009:4738036] -[UIView setDate:]: unrecognized selector sent to instance 0x7feaf8f9cbf0
2015-11-23 22:34:45.151 DatePicker xxTestxx[19009:4738036] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setDate:]: unrecognized selector sent to instance 0x7feaf8f9cbf0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102e75f45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000104b99deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000102e7e56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000102dcbeea ___forwarding___ + 970
    4   CoreFoundation                      0x0000000102dcba98 _CF_forwarding_prep_0 + 120
    5   DatePicker xxTestxx                 0x0000000102c8e944 _TFC19DatePicker_xxTestxx14ViewController11viewDidLoadfS0_FT_T_ + 260
    6   DatePicker xxTestxx                 0x0000000102c8e9e2 _TToFC19DatePicker_xxTestxx14ViewController11viewDidLoadfS0_FT_T_ + 34
    7   UIKit                               0x0000000103824cc4 -[UIViewController loadViewIfRequired] + 1198
    8   UIKit                               0x0000000103825013 -[UIViewController view] + 27
    9   UIKit                               0x00000001036fe51c -[UIWindow addRootViewControllerViewIfPossible] + 61
    10  UIKit                               0x00000001036fec05 -[UIWindow _setHidden:forced:] + 282
    11  UIKit                               0x00000001037104a5 -[UIWindow makeKeyAndVisible] + 42
    12  UIKit                               0x000000010368a396 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4131
    13  UIKit                               0x00000001036909c3 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1750
    14  UIKit                               0x000000010368dba3 -[UIApplication workspaceDidEndTransaction:] + 188
    15  FrontBoardServices                  0x0000000106a28784 -[FBSSerialQueue _performNext] + 192
    16  FrontBoardServices                  0x0000000106a28af2 -[FBSSerialQueue _performNextFromRunLoopSource] + 45
    17  CoreFoundation                      0x0000000102da2011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    18  CoreFoundation                      0x0000000102d97f3c __CFRunLoopDoSources0 + 556
    19  CoreFoundation                      0x0000000102d973f3 __CFRunLoopRun + 867
    20  CoreFoundation                      0x0000000102d96e08 CFRunLoopRunSpecific + 488
    21  UIKit                               0x000000010368d4f5 -[UIApplication _run] + 402
    22  UIKit                               0x000000010369230d UIApplicationMain + 171
    23  DatePicker xxTestxx                 0x0000000102c9120d main + 109
    24  libdyld.dylib                       0x00000001056a192d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

The error message is very clear that you are calling setDate on UIView .错误消息非常清楚,您是在UIView上调用setDate

In your code, the only place setDate is called is this line在您的代码中,唯一调用setDate的地方是这一行

 datePicker.date = NSDate().dateByAddingTimeInterval(1000) // initialize it to whatever you want

(Note: Compiler will compile x.date = y to x.setDate(y) ) (注意:编译器会将x.date = y编译为x.setDate(y)

Which means datePicker.date is UIView instead of UIDatePicker .这意味着datePicker.dateUIView而不是UIDatePicker

You have to change the nib file in interface builder to make sure the view that connect to datePicker is UIDatePicker instead of UIView您必须更改界面构建器中的 nib 文件以确保连接到datePicker的视图是UIDatePicker而不是UIView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM