无法转换(计时器!)->无效到((CFRunLoopTimer?)->无效)!- 将 NSTimer 扩展转换为 Swift 3 [英] Cannot convert (Timer!) -> Void to ((CFRunLoopTimer?) ->Void)! - Converting NSTimer extension to Swift 3
问题描述
我正在尝试将我在我的项目中使用的 Pod 转换为 Swift 3.它不是我写的,但原作者没有更新它,所以我把它分叉了,我试图自己做.但是...
I am trying to convert a Pod I am using in my project to Swift 3. I didn't write it, but the original author has not updated it so I forked it any I'm trying to do it myself. But...
我在尝试将扩展到 NSTimer
转换为 Swift 3 时遇到此错误:无法转换类型'(Timer!) -> 的值Void' 到预期的参数类型 '((CFRunLoopTimer?) -> Void)!
I get this error trying to convert an extension to NSTimer
to Swift 3:
Cannot convert value of type '(Timer!) -> Void' to expected argument type '((CFRunLoopTimer?) -> Void)!
似乎是 Swift 3 处理程序类型,(Timer!) ->Void
与旧式 CFRunLoop
样式处理程序不兼容,但我不确定如何在保持与 iOS 9 兼容的同时进行转换.
It seems that the Swift 3 handler type, (Timer!) -> Void
is not compatible with the old school CFRunLoop
style handlers, but I am not sure how to convert this over while maintaining compatibility with iOS 9.
我正在粘贴以下代码,由 Xcode 转换.您可以在 https://github.com/找到原始代码entotsu/TKSubmitTransition/blob/master/SubmitTransition/Classes/NSTimerEx.swift
I am pasting the code below, as converted by Xcode. You can find the original code at https://github.com/entotsu/TKSubmitTransition/blob/master/SubmitTransition/Classes/NSTimerEx.swift
干杯
import Foundation
extension Timer {
class func schedule(delay delay: TimeInterval, handler: (Timer!) -> Void) -> NSTimer {
let fireDate = delay + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, 0, 0, 0, handler) // Error on this line
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
return timer
}
class func schedule(repeatInterval interval: TimeInterval, handler: @escaping (Timer!) -> Void) -> Timer {
let fireDate = interval + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, interval, 0, 0, handler) // And this line
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes)
return timer
}
}
推荐答案
试试这个:
extension Timer {
class func schedule(delay: TimeInterval, handler: ((Timer?) -> Void)!) -> Timer! {
let fireDate = delay + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, 0, 0, 0, handler)
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, .commonModes)
return timer
}
class func schedule(repeatInterval interval: TimeInterval, handler: ((Timer?) -> Void)!) -> Timer! {
let fireDate = interval + CFAbsoluteTimeGetCurrent()
let timer = CFRunLoopTimerCreateWithHandler(kCFAllocatorDefault, fireDate, interval, 0, 0, handler)
CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, .commonModes)
return timer
}
}
这篇关于无法转换(计时器!)->无效到((CFRunLoopTimer?)->无效)!- 将 NSTimer 扩展转换为 Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!