Skip to content

Instantly share code, notes, and snippets.

@alexj70
Last active April 22, 2017 19:13
Show Gist options
  • Save alexj70/58bb1eebf8fd559c9aa1af1c4f11e8a4 to your computer and use it in GitHub Desktop.
Save alexj70/58bb1eebf8fd559c9aa1af1c4f11e8a4 to your computer and use it in GitHub Desktop.
Cached formatter
import Foundation
import Contacts
import MapKit
// MARK: - Distatnce formatter
fileprivate let _distanceFormatter: MKDistanceFormatter = {
let formatter = MKDistanceFormatter()
return formatter
}()
extension MKDistanceFormatter {
/// The default application Distance Formatter
static var `default`: MKDistanceFormatter { return _distanceFormatter }
}
// MARK: - Date formatter
fileprivate let _shortDateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
return dateFormatter
}()
extension DateFormatter {
/// The default application Date Formatter, Returns in localized "Oct 02, 1970" format
static var `default`: DateFormatter {
let template = DateFormatter.dateFormat(fromTemplate: "dd MMM yyyy", options: 0, locale: Settings.locale)
_shortDateFormatter.setLocalizedDateFormatFromTemplate(template!)
return _shortDateFormatter
}
}
// MARK: - Number formatter
fileprivate let _numberFormatter: NumberFormatter = {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = Locale(identifier: "de_DE")
return numberFormatter
}()
extension NumberFormatter {
/// Returns default formatter for the app
static var `default` : NumberFormatter { return _numberFormatter }
}
// MARK: - Postal address
fileprivate let _postalAddressFormatter = CNPostalAddressFormatter()
fileprivate let _postalAddressDefaultSeparator = "\n"
extension CNPostalAddressFormatter {
/// Returns default Postal address formatter for the app
static var `default` : CNPostalAddressFormatter { return _postalAddressFormatter }
/// Formattes postal address, separeted by 'separator'. The defaul separator is '\n'
///
/// - Parameters:
/// - address: Core data model
/// - separator: the lines will separeted by
/// - Returns: formatted postal address
public func string(from address: Address?, separetedBy separator: String = _postalAddressDefaultSeparator ) -> String? {
guard let address = address else {
return nil
}
let postalAddress = CNMutablePostalAddress()
if let street = address.street {
postalAddress.street = street.trimmingCharacters(in: .whitespacesAndNewlines)
}
// For the Gemany localization it is neccessary to replace ZIP with City Name.
// Otherwise uses the native implementation
if Locale.preferredLocale.identifier == "de" {
if let city = address.city?.name {
postalAddress.postalCode = city.trimmingCharacters(in: .whitespacesAndNewlines)
}
if let zip = address.zip {
postalAddress.city = zip.trimmingCharacters(in: .whitespacesAndNewlines)
}
} else {
if let city = address.city?.name {
postalAddress.city = city.trimmingCharacters(in: .whitespacesAndNewlines)
}
if let zip = address.zip {
postalAddress.postalCode = zip.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
if let country = address.city?.country?.name {
postalAddress.country = country.trimmingCharacters(in: .whitespacesAndNewlines)
}
let str = self.string(from: postalAddress).trimmingCharacters(in: .whitespacesAndNewlines).replacingOccurrences(of: " ", with: " ")
if separator == _postalAddressDefaultSeparator {
return str
} else {
return str.replacingOccurrences(of: _postalAddressDefaultSeparator, with: separator)
}
}
}
extension Locale {
public static var preferredLocale: Locale {
guard let localization = Bundle.main.preferredLocalizations.first else {
return Locale.current
}
return Locale(identifier: localization)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment