스위프트 4에서 타이머를 사용할 방법을 찾고 있었고 this 기사를 보았습니다. xcode에서 내 코드를 테스트하고 타이머가 처음 틱 (이 경우 10 초 후)되면 응용 프로그램이 다운되고 빌드가 성공했지만 오류가 발생합니다.NSException으로 스위프트 4 타이머가 충돌 함
2017-11-20 19:54:42.781502-0700 Rock Prodigy[3022:554505] -[_SwiftValue tick]: unrecognized selector sent to instance 0x608000051520
2017-11-20 19:54:42.791278-0700 Rock Prodigy[3022:554505] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue tick]: unrecognized selector sent to instance 0x608000051520'
*** First throw call stack:
(
0 CoreFoundation 0x000000010360d1ab __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000102ca2f41 objc_exception_throw + 48
2 CoreFoundation 0x000000010368da34 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00000001035900a8 ___forwarding___ + 1432
4 CoreFoundation 0x000000010358fa88 _CF_forwarding_prep_0 + 120
5 Foundation 0x000000010270e1ee __NSFireTimer + 83
6 CoreFoundation 0x000000010359d2a4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
7 CoreFoundation 0x000000010359cf62 __CFRunLoopDoTimer + 1026
8 CoreFoundation 0x000000010359cb1a __CFRunLoopDoTimers + 266
9 CoreFoundation 0x0000000103594534 __CFRunLoopRun + 2308
10 CoreFoundation 0x00000001035939b9 CFRunLoopRunSpecific + 409
11 GraphicsServices 0x00000001090e29c6 GSEventRunModal + 62
12 UIKit 0x0000000103a885e8 UIApplicationMain + 159
13 Rock Prodigy 0x000000010238b637 main + 55
14 libdyld.dylib 0x0000000107aa1d81 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
여기에 4 코드
import UIKit
class GeneralFitness: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var timer: Timer! = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
@objc func tick() {
print("tick")
}
}
문의 사항이 있으면 알려주세요 내 신속하다.
귀하의 타이머가 잘못된 방법으로 초기화
위대한 리드! 스위프트 4에서는 viewDidAppear (그렇지 않으면 내 코드가 추락)로 타이머 설정을 이동해야했습니다. –