Vì vậy, tôi vừa cập nhật để Xcode 8 và chuyển đổi Swift 2.3 mã của tôi để Swift 3, và Swift 3 chỉ chuyển đổi tất cả các NSDate
-Foundation.Date
NSDate từ Swift 2.3 đến ngày từ 3 chuyển đổi Swift
Và tôi đã có một extension
cho NSDate
, bây giờ gọi là Foundation.Date
, nó trông như thế này:
extension NSDate
{
var firstDayOfTheMonth:NSDate
{
var date: NSDate?
NSCalendar.currentCalendar().rangeOfUnit(.Month, startDate:&date , interval: nil, forDate: self)
return date!
}
}
Và Swift 3 chuyển đổi nó như thế này:
extension Foundation.Date
{
var firstDayOfTheMonth:Foundation.Date
{
var date: Foundation.Date?
(Calendar.current as NSCalendar).range(of: .month, start:&date , interval: nil, for: self)
return date!
}
}
Vấn đề ở đây là Xcode gợi ý tôi làm điều này: Cannot convert value of type 'Date?' to expected argument type 'NSDate?'
và để thả &
.
Vì vậy, tôi thay đổi nó và chức năng bây giờ trông như thế này:
extension Foundation.Date
{
var firstDayOfTheMonth:Foundation.Date
{
var date: Foundation.Date?
(Calendar.current as NSCalendar).range(of: .month, start:date as NSDate?, interval: nil, for: self)
return date!
}
}
Nhưng bây giờ Xcode nói Cannot convert value of type 'Date?' to type 'NSDate?' in coercion
Làm thế nào tôi có thể làm công việc này?
'rangeOfUnit' là' dateInterval' trong Swift 3, xem ví dụ http://stackoverflow.com/a/39527482/1187415. –
Bạn nói đúng Martin R, cảm ơn. –