Skip to main content
Rollback to Revision 3
Link
rmaddy
  • 195
  • 7

Translating Objective-C use of static and +(void)initialize to Swift versions of a month name formatter

edited tags; edited title
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Translating Objective-C use of static and +(void)initialize to Swift versions of a month name formatter

Rollback to Revision 1
Source Link
rmaddy
  • 195
  • 7
#import <Foundation/Foundation.h>

@interface RMYearInfo : NSObject

// There are also some instance properties but those aren't relevant to the question

+ (NSInteger)numberOfMonths;
+ (NSArray *)shortMonthNames;
+ (NSArray *)longMonthNames;
+ (NSInteger)firstWeekday;
// several other class methods

@end
#import "DateInfo.h"

static NSArray *shortMonthNames = nil;
static NSArray *longMonthNames = nil;
static NSInteger firstWeekday;
// there are several other statics as well

@implementation DateInfo

+ (void)reinitialize {
    NSCalendar *cal = [NSCalendar currentCalendar];
    firstWeekday = [cal firstWeekday] - 1;
    
    NSDateFormatter *yearFormatter = [[NSDateFormatter alloc] init];

    NSArray *shortMonthsshortMonthNames = [yearFormatter shortStandaloneMonthSymbols];
    NSArray *longMonthslongMonthNames = [yearFormatter standaloneMonthSymbols];
    NSMutableArray *upperShortMonths = [[NSMutableArray alloc] initWithCapacity:shortMonths.count];
    NSMutableArray *upperLongMonths = [[NSMutableArray alloc] initWithCapacity:shortMonths.count];
    for (NSString *name in shortMonths) {
        [upperShortMonths addObject:[name uppercaseString]];
    }
    for (NSString *name in longMonths) {
        [upperLongMonths addObject:[name uppercaseString]];
    }
    shortMonthNames = [upperShortMonths copy];
    longMonthNames = [upperLongMonths copy];

    NSLocale *locale = [NSLocale current];
    // lots of other processing for the other statics
    // based on yearFormatter, locale, and cal
}

+ (void)initialize {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reinitialize) name:NSCurrentLocaleDidChangeNotification object:nil];
    
    [self reinitialize];
}

+ (NSInteger)numberOfMonths {
    return shortMonthNames.count;
}

+ (NSArray *)shortMonthNames {
    return shortMonthNames;
}

+ (NSArray *)longMonthNames {
    return longMonthNames;
}
 
+ (NSInteger)firstWeekday {
    return firstWeekday;
}

// Lots of other instance and class methods
import Foundation

public struct DateInfo {
    // some normal instance properties irrelevant to the question

    private static var _formatter: DateFormatter!
    private static var formatter: DateFormatter {
        if _formatter == nil {
            _formatter = DateFormatter()
            NotificationCenter.default.addObserver(forName: NSLocale.currentLocaleDidChangeNotification, object: nil, queue: nil) { (notification) in
                _formatter = DateFormatter()
                _shortMonthNames = nil
                _longMonthNames = nil
                _firstWeekday = nil
                // reset all of the other statics as well
            }
        }

        return _formatter
    }

    private static var _shortMonthNames: [String]!
    public static var shortMonthNames: [String] {
        if _shortMonthNames == nil {
            // Processing is actually more complex than this simple assignment
            _shortMonthNames = formatter.shortStandaloneMonthSymbols
        }

        return _shortMonthNames
    }

    private static var _longMonthNames: [String]!
    public static var longMonthNames: [String] {
        if _longMonthNames == nil {
            // Processing is actually more complex than this simple assignment
            _longMonthNames = formatter.standaloneMonthSymbols
        }

        return _longMonthNames
    }

    public static var numberOfMonths: Int {
        return shortMonthNames.count
    }

    private static var _firstWeekday: Int!
    public static var firstWeekday: Int {
        if _firstWeekday == nil {
            _firstWeekday = Calendar.current.firstWeekday - 1
        }

        return _firstWeekday
    }

    // lots of other similar private/public pairs of statics
}
#import <Foundation/Foundation.h>

@interface RMYearInfo : NSObject

// There are also some instance properties but those aren't relevant to the question

+ (NSInteger)numberOfMonths;
+ (NSArray *)shortMonthNames;
+ (NSArray *)longMonthNames;
+ (NSInteger)firstWeekday;
// several other class methods

@end
#import "DateInfo.h"

static NSArray *shortMonthNames = nil;
static NSArray *longMonthNames = nil;
static NSInteger firstWeekday;
// there are several other statics as well

@implementation DateInfo

+ (void)reinitialize {
    NSCalendar *cal = [NSCalendar currentCalendar];
    firstWeekday = [cal firstWeekday] - 1;
    
    NSDateFormatter *yearFormatter = [[NSDateFormatter alloc] init];

    NSArray *shortMonths = [yearFormatter shortStandaloneMonthSymbols];
    NSArray *longMonths = [yearFormatter standaloneMonthSymbols];
    NSMutableArray *upperShortMonths = [[NSMutableArray alloc] initWithCapacity:shortMonths.count];
    NSMutableArray *upperLongMonths = [[NSMutableArray alloc] initWithCapacity:shortMonths.count];
    for (NSString *name in shortMonths) {
        [upperShortMonths addObject:[name uppercaseString]];
    }
    for (NSString *name in longMonths) {
        [upperLongMonths addObject:[name uppercaseString]];
    }
    shortMonthNames = [upperShortMonths copy];
    longMonthNames = [upperLongMonths copy];

    NSLocale *locale = [NSLocale current];
    // lots of other processing for other statics
    // based on yearFormatter, locale, and cal
}

+ (void)initialize {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reinitialize) name:NSCurrentLocaleDidChangeNotification object:nil];
    
    [self reinitialize];
}

+ (NSInteger)numberOfMonths {
    return shortMonthNames.count;
}

+ (NSArray *)shortMonthNames {
    return shortMonthNames;
}

+ (NSArray *)longMonthNames {
    return longMonthNames;
}
 
+ (NSInteger)firstWeekday {
    return firstWeekday;
}

// Lots of other instance and class methods
import Foundation

public struct DateInfo {
    // some normal instance properties irrelevant to the question

    private static var _formatter: DateFormatter!
    private static var formatter: DateFormatter {
        if _formatter == nil {
            _formatter = DateFormatter()
            NotificationCenter.default.addObserver(forName: NSLocale.currentLocaleDidChangeNotification, object: nil, queue: nil) { (notification) in
                _formatter = DateFormatter()
                _shortMonthNames = nil
                _longMonthNames = nil
                _firstWeekday = nil
                // reset all of the other statics as well
            }
        }

        return _formatter
    }

    private static var _shortMonthNames: [String]!
    public static var shortMonthNames: [String] {
        if _shortMonthNames == nil {
            // Processing is actually more complex than this simple assignment
            _shortMonthNames = formatter.shortStandaloneMonthSymbols
        }

        return _shortMonthNames
    }

    private static var _longMonthNames: [String]!
    public static var longMonthNames: [String] {
        if _longMonthNames == nil {
            // Processing is actually more complex than this simple assignment
            _longMonthNames = formatter.standaloneMonthSymbols
        }

        return _longMonthNames
    }

    public static var numberOfMonths: Int {
        return shortMonthNames.count
    }

    private static var _firstWeekday: Int!
    public static var firstWeekday: Int {
        if _firstWeekday == nil {
            _firstWeekday = Calendar.current.firstWeekday - 1
        }

        return _firstWeekday
    }

    // lots of other similar private/public pairs of statics
}
#import <Foundation/Foundation.h>

@interface RMYearInfo : NSObject

// There are also some instance properties but those aren't relevant to the question

+ (NSInteger)numberOfMonths;
+ (NSArray *)shortMonthNames;
+ (NSArray *)longMonthNames;
// several other class methods

@end
#import "DateInfo.h"

static NSArray *shortMonthNames = nil;
static NSArray *longMonthNames = nil;
// there are several other statics as well

@implementation DateInfo

+ (void)reinitialize {
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    NSDateFormatter *yearFormatter = [[NSDateFormatter alloc] init];

    shortMonthNames = [yearFormatter shortStandaloneMonthSymbols];
    longMonthNames = [yearFormatter standaloneMonthSymbols];

    // lots of other processing for the other statics
}

+ (void)initialize {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reinitialize) name:NSCurrentLocaleDidChangeNotification object:nil];
    
    [self reinitialize];
}

+ (NSInteger)numberOfMonths {
    return shortMonthNames.count;
}

+ (NSArray *)shortMonthNames {
    return shortMonthNames;
}

+ (NSArray *)longMonthNames {
    return longMonthNames;
}

// Lots of other instance and class methods
import Foundation

public struct DateInfo {
    // some normal instance properties irrelevant to the question

    private static var _formatter: DateFormatter!
    private static var formatter: DateFormatter {
        if _formatter == nil {
            _formatter = DateFormatter()
            NotificationCenter.default.addObserver(forName: NSLocale.currentLocaleDidChangeNotification, object: nil, queue: nil) { (notification) in
                _formatter = DateFormatter()
                _shortMonthNames = nil
                _longMonthNames = nil
                // reset all of the other statics as well
            }
        }

        return _formatter
    }

    private static var _shortMonthNames: [String]!
    public static var shortMonthNames: [String] {
        if _shortMonthNames == nil {
            _shortMonthNames = formatter.shortStandaloneMonthSymbols
        }

        return _shortMonthNames
    }

    private static var _longMonthNames: [String]!
    public static var longMonthNames: [String] {
        if _longMonthNames == nil {
            _longMonthNames = formatter.standaloneMonthSymbols
        }

        return _longMonthNames
    }

    public static var numberOfMonths: Int {
        return shortMonthNames.count
    }

    // lots of other similar private/public pairs of statics
}
added 526 characters in body
Source Link
rmaddy
  • 195
  • 7
Loading
Source Link
rmaddy
  • 195
  • 7
Loading