PGLFormatter is a simple container of NSFormatter and useful functions
##Reason Formatters are extremely expensive to create so it's very important to reuse the formatter as much as you can.
This project is inspired from this article published on NSHipster
If you use the same formatter in the entire project you can set in a single point (e.g. AppDelegate) and then forget about the NSFormatter and simple use the function that PGLFormatter offers, for format() to format Double Float Int (see the example), otherwise you should remember to set the behavior every time
To integrate PGLFormatter into your Xcode project using Cocoapods, specify it in your Podfile:
use_frameworks!
pod 'PGLFormatter'
To integrate PGLFormatter into your Xcode project using Carthage, specify it in your Cartfile:
github "PGLongo/PGLFormatter"
let price:Double = 12356789.4200
var formatter = PGLFormatter.numberFormatter
// set the formatter behavior
formatter.numberStyle = .CurrencyStyle
price.format() // $12,356,789.42
var formatter = PGLFormatter.byteFormatter
// set the formatter behavior
42.formatToByte() // "42 bytes"
42000.formatToByte() // "42 KB"
Int64(42000000000).formatToByte() // 42 GB
var formatter = PGLFormatter.lenghtFormatter
// set the formatter behavior
42.formatLenght(.Meter) // 42 m
42.formatLenghtFromMeters() // 45.931 yd
var formatter = PGLFormatter.massFormatter
// set the formatter behavior
42.formatMass(.Gram) // 42 g
42.formatMassFromKilograms() // 92.594 lb
let calendar = NSCalendar.currentCalendar()
let components = NSDateComponents()
components.year = 1988
components.month = 11
components.day = 30
components.hour = 0
components.minute = 0
components.second = 0
let date = calendar.dateFromComponents(components)
date.format() // 11/30/88, 12:00 AM
PGLFormatter.timeFormatter
formatter.unitsStyle = .Abbreviated
// set the formatter behavior
42.formatTime()! // 42s
4200.formatTime()! // 1h 10m
Documentation is generated and manteined by Cocoapods. You can read the latest documentation at the following URL and add the docs to Dash.
- Support Cocoapods
- Support Carthage
- Add installation instruction
- Add other formatters
- Add documentation
- Implements other useful functions
PGLFormatter is released under the MIT license. See LICENSE for details.