How to Organize Formatters Across Different Source Code Files

joooerhooo

Is there a good/neat way to organize formatters that are being repeated across different files? I find myself having to implement the same lines of code for different classes. Should I just put the repeated code in a separate file and give it global access? Is it better to create a class/struct to hold these formatters? I may be overthinking this but I would like to learn a good method and stick to it as I work on different projects.

For example, the code being repeated looks like this:

let dateFormatter: DateFormatter = {
   let formatter = DateFormatter()
   formatter.dateFormat = "MM/dd/yyyy"
   return formatter
}()
Leo Dabus

You can extend Formatter class and declare your date formatters as static properties. This way you can access the same instance of them anywhere in your code. Note that you should not use a fixed format when displaying the date to the user. You should use date and time style to display it localized based on the user device locale and settings. Btw when using a fixed date format you should set the locale to "en_US_POSIX":

extension Formatter {
    static let mmddyyyy: DateFormatter = {
        let formatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.dateFormat = "MM/dd/yyyy"
        return formatter
    }()
    static let shortDate: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .short
        return formatter
    }()
    static let mediumDate: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateStyle = .medium
        return formatter
    }()
}

Formatter.mmddyyyy.string(from: Date())      // "06/24/2020"
Formatter.shortDate.string(from: Date())     // "6/24/20"
Formatter.mediumDate.string(from: Date())    // "Jun 24, 2020"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to organize Android source code into folders

How do I create different folders to organize source code in Android Studio?

Vertx Web: How to split and organize routes across multiple files?

How do you organize class source code in Java?

How can I organize a large number of Stringtemplate files into different folders

How to organize image files of different sizes (devices) in a Appcelerator Titanium project

How to organize Webpack 5 files for different modes with same plugins

how to organize my code into multiple files to share a common global variable

How do I organize my Processing code in separate files?

How to organize files in linux?

How to batch organize files?

How to organize my code?

How to organize CakePHP code?

How to organize assembly code?

How to share a variable across multiple source files in C++?

How to organize functionality across react components?

How to use source map files on different OS

How to configure code formatters Beautify, Prettier, per project in VS Code

How can I share variable data across different files in React

How to use Vim to edit files using SCP but across different Subnets?

How to organize header files in a library

how to organize JS files in Django?

C++ | spdlog | How to have sincs with different formatters?

python multiprocessing across different files

different icons for generic source-code and other files

Editing previous source files to run different code on visual studio

Importing files from different folders with Haskell source code

Lambda function - How to split python code across multiple files

How to improve node js code for production and split it properly across files?