Swift 4 和 Xcode 9:无法使用类型为“(NSCalendar.Unit.Type, from: NSData, to: Date?)”的参数列表调用 'dateComponents'
Swift 4 and Xcode 9: Cannot invoke 'dateComponents' with an argument list of type '(NSCalendar.Unit.Type, from: NSData, to: Date?)'
正在尝试创建倒数计时器,但我无法解决此错误:
Cannot invoke 'dateComponents' with an argument list of type '(NSCalendar.Unit.Type, from: NSData, to: Date?)'
我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var TimerLable: UILabel!
let formatter = DateFormatter()
let userCleander = Calendar.current;
let CalenderComponent : NSCalendar.Unit = [
NSCalendar.Unit.year,
NSCalendar.Unit.month,
NSCalendar.Unit.day,
NSCalendar.Unit.hour
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func printTime()
{
formatter.dateFormat = "dd/MM/yy hh:mm a"
let StartTime = NSData()
let EndTime = formatter.date(from: "10/08/19 12:00 a")
let TimeDifference = userCleander.dateComponents(NSCalendar.Unit, from: StartTime, to: EndTime)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
导致错误的主要问题只是一个错字。 StartTime
应该是 NSDate
而不是 NSData
.
如果有 Swift 本地对应项(Calendar
、Date
等),基本上不要使用 NS...
类。
Swift 3+ 代码是
let formatter = DateFormatter()
let userCalendar = Calendar.current
let calendarComponents : Set<Calendar.Component> = [ .year, .month, .day, .hour]
func printTime()
{
formatter.dateFormat = "dd/MM/yy hh:mm a"
let startTime = Date()
let endTime = formatter.date(from: "10/08/19 12:00 am")!
let timeDifference = userCalendar.dateComponents(calendarComponents, from: startTime, to: endTime)
}
两个注意事项:
- 根据命名准则,变量名以小写字母开头
- 日期格式与日期字符串不匹配,创建的日期需要解包。
正在尝试创建倒数计时器,但我无法解决此错误:
Cannot invoke 'dateComponents' with an argument list of type '(NSCalendar.Unit.Type, from: NSData, to: Date?)'
我的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var TimerLable: UILabel!
let formatter = DateFormatter()
let userCleander = Calendar.current;
let CalenderComponent : NSCalendar.Unit = [
NSCalendar.Unit.year,
NSCalendar.Unit.month,
NSCalendar.Unit.day,
NSCalendar.Unit.hour
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func printTime()
{
formatter.dateFormat = "dd/MM/yy hh:mm a"
let StartTime = NSData()
let EndTime = formatter.date(from: "10/08/19 12:00 a")
let TimeDifference = userCleander.dateComponents(NSCalendar.Unit, from: StartTime, to: EndTime)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
导致错误的主要问题只是一个错字。 StartTime
应该是 NSDate
而不是 NSData
.
如果有 Swift 本地对应项(Calendar
、Date
等),基本上不要使用 NS...
类。
Swift 3+ 代码是
let formatter = DateFormatter()
let userCalendar = Calendar.current
let calendarComponents : Set<Calendar.Component> = [ .year, .month, .day, .hour]
func printTime()
{
formatter.dateFormat = "dd/MM/yy hh:mm a"
let startTime = Date()
let endTime = formatter.date(from: "10/08/19 12:00 am")!
let timeDifference = userCalendar.dateComponents(calendarComponents, from: startTime, to: endTime)
}
两个注意事项:
- 根据命名准则,变量名以小写字母开头
- 日期格式与日期字符串不匹配,创建的日期需要解包。