Xcode 8 beta 4: Calendar.Unit vs Calendar.Component

后端 未结 2 1836
感情败类
感情败类 2020-12-29 04:13

This Swift 3 code worked until XCode 8 beta 3:

let calendar = Calendar.current
let anchorComponents = calendar.components([Calendar.Unit.day, Calendar.Unit.m         


        
相关标签:
2条回答
  • 2020-12-29 04:25

    In the Swift version shipped with Xcode 8 beta 4, components has been renamed to dateComponents.

    Note: to make the call simpler, you can omit the Calendar.Component prefix.

    let anchorComponents = calendar.dateComponents([.day, .month, .year, .hour], from: self)
    

    The error message you got is a bit misleading, I guess the compiler was struggling with type inference.

    0 讨论(0)
  • 2020-12-29 04:38

    Swift 3 and Swift 4:

    In this example you could also prepare your unitFlags from/to date:

    let calendar = NSCalendar.current
    let unitFlags = Set<Calendar.Component>([.day, .month, .year, .hour])
    let anchorComponents = calendar.dateComponents(unitFlags, from: startDate as Date,  to: endDate as Date)
    
    0 讨论(0)
提交回复
热议问题