最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift代码警告:NSYearCalendarUnit was deprecated in iOS 8.0 Use NSCalendarUnitYear instead

Swift crifan 3534浏览 0评论

【背景】

xcode中代码编译出错:

swift:80:56: ‘NSYearCalendarUnit’ was deprecated in iOS 8.0: Use NSCalendarUnitYear instead

如图:

NSYearCalendarUnit was deprecated in iOS 8.0 Use NSCalendarUnitYear instead

 

【解决过程】

1.搜:

swift NSYearCalendarUnit was deprecated in iOS 8.0 Use NSCalendarUnitYear instead

看起来是api更新,旧的过期了。

2.所以去网上搜搜,

swift NSYearCalendarUnit was deprecated in iOS 8.0 Use NSCalendarUnitYear instead

参考:

xcode – NSYearCalendarUnit deprecated in iOS – Stack Overflow

去换用

NSCalendarUnitYear

去试试。

3.然后通过,按住alt后,点击 NSCalendarUnit,进去看了源码:

public struct NSCalendarUnit : OptionSetType {
    public init(rawValue: UInt)
   
    public static var Era: NSCalendarUnit { get }
    public static var Year: NSCalendarUnit { get }
    public static var Month: NSCalendarUnit { get }
    public static var Day: NSCalendarUnit { get }
    public static var Hour: NSCalendarUnit { get }
    public static var Minute: NSCalendarUnit { get }
    public static var Second: NSCalendarUnit { get }
    public static var Weekday: NSCalendarUnit { get }
    public static var WeekdayOrdinal: NSCalendarUnit { get }
    @available(iOS 4.0, *)
    public static var Quarter: NSCalendarUnit { get }
    @available(iOS 5.0, *)
    public static var WeekOfMonth: NSCalendarUnit { get }
    @available(iOS 5.0, *)
    public static var WeekOfYear: NSCalendarUnit { get }
    @available(iOS 5.0, *)
    public static var YearForWeekOfYear: NSCalendarUnit { get }
    @available(iOS 5.0, *)
    public static var Nanosecond: NSCalendarUnit { get }
    @available(iOS 4.0, *)
    public static var Calendar: NSCalendarUnit { get }
    @available(iOS 4.0, *)
    public static var TimeZone: NSCalendarUnit { get }

很明显,没有那些

NSCalendarUnitYear

之类但,直接都改为对应的变量,即可。

改为:

        let calendar:NSCalendar = NSCalendar.currentCalendar()
        let unitFlags:NSCalendarUnit = [NSCalendarUnit.Year, NSCalendarUnit.Month, NSCalendarUnit.Day, NSCalendarUnit.Hour, NSCalendarUnit.Minute]

即可。

4.另外也看到:

Questions about NSCalendar | Apple Developer Forums

也是这么说的。。

 

【总结】

‘NSYearCalendarUnit’ was deprecated in iOS 8.0: Use NSCalendarUnitYear instead

很明显是API更新,NSYearCalendarUnit换成NSCalendarUnitYear了。

结果是,再去把对应的域,修改为对应的,符合对应的库里面的内容:

比如,NSCalendarUnit.Month,即可。

转载请注明:在路上 » 【已解决】swift代码警告:NSYearCalendarUnit was deprecated in iOS 8.0 Use NSCalendarUnitYear instead

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.184 seconds, using 22.10MB memory