获得当年

Jay*_*Jay 5 cocoa objective-c

只是出于好奇(因为这是我第一次需要在Cocoa中计算它) -
真的是计算当年最简洁的方法吗?

int thisYear = [[[NSCalendar currentCalendar] 
                components:NSYearCalendarUnit fromDate:[NSDate date]] 
               year];
Run Code Online (Sandbox Code Playgroud)

看起来很笨拙..

Dan*_*iel 4

是的,当我需要从日期(如日、月或年)获取组件时,我使用NSDateComponents,事实上日期很笨拙 - 有不同的日历、时区、闰年等。