2017-04-27 66 views
0

定製firstWeekday有沒有可能性? 我應該繼承NSCalendar嗎?NSCalendar - 全球首個週日設置

現在我必須設置它像這樣在每一個地方,我需要使用NSCalendar

NSCalendar* calendar = [NSCalendar currentCalendar]; 
calendar.firstWeekday = [NSUserDefaultsHelper firstDayOfWeek]; 

回答

1

你可以寫的NSCalendar一類帶有水上實例變量:

@interface NSCalendar (WeekdayCalendar) 

+ (NSCalendar *)customWeekdayCalendar; 

@end 

@implementation NSCalendar (WeekdayCalendar) 

+ (NSCalendar *)customWeekdayCalendar 
{ 
    static NSCalendar *customCalendar = nil; 
    if (customCalendar == nil) { 
     customCalendar = [self currentCalendar]; 
     customCalendar.firstWeekday = [NSUserDefaultsHelper firstDayOfWeek]; 
    } 
    return customCalendar; 
} 

@end 

然後你可以使用任何地方的日曆

NSCalendar *myCalendar = [NSCalendar customWeekdayCalendar];