Latest 3.2.0
Homepage https://github.com/PGLongo/PGLFormatter
License MIT
Platforms ios 8.0, watchos 2.0

PGLFormatter is a simple container of NSFormatter and useful functions
Build Status
Carthage compatible
CocoaPods

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

Add PGLFormatter to your project

Cocoapods

To integrate PGLFormatter into your Xcode project using Cocoapods, specify it in your Podfile:

use_frameworks!
pod 'PGLFormatter'

Carthage

To integrate PGLFormatter into your Xcode project using Carthage, specify it in your Cartfile:

github "PGLongo/PGLFormatter"

Example

Double Float Int

let price:Double = 12356789.4200

var formatter = PGLFormatter.numberFormatter
// set the formatter behavior
formatter.numberStyle = .CurrencyStyle

price.format() // $12,356,789.42

Formatting Byte

var formatter = PGLFormatter.byteFormatter
// set the formatter behavior

42.formatToByte() // "42 bytes"
42000.formatToByte() // "42 KB"
Int64(42000000000).formatToByte() // 42 GB

Formatting Lenght

var formatter = PGLFormatter.lenghtFormatter
// set the formatter behavior

42.formatLenght(.Meter) // 42 m
42.formatLenghtFromMeters() // 45.931 yd

Formatting Mass

var formatter = PGLFormatter.massFormatter
// set the formatter behavior

42.formatMass(.Gram) // 42 g
42.formatMassFromKilograms() // 92.594 lb

NSDate

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

TimeFormatter

PGLFormatter.timeFormatter
formatter.unitsStyle = .Abbreviated
// set the formatter behavior

42.formatTime()! // 42s
4200.formatTime()! // 1h 10m

Documentation

Documentation is generated and manteined by Cocoapods. You can read the latest documentation at the following URL and add the docs to Dash.

Task

  • [x] Support Cocoapods
  • [x] Support Carthage
  • [x] Add installation instruction
  • [x] Add other formatters
  • [x] Add documentation
  • [ ] Implements other useful functions

License

PGLFormatter is released under the MIT license. See LICENSE for details.

Latest podspec

{
    "name": "PGLFormatter",
    "version": "3.2.0",
    "summary": "Simple container of NSFormatter and useful functions to format numbers and dates.",
    "description": "# PGLFormatternPGLFormatter is a simple container of NSFormatter and useful functions  n##ReasonnFormatters are extremely expensive to create so it's very important to reuse the formatter as much as you can. nnIf 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",
    "homepage": "https://github.com/PGLongo/PGLFormatter",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": "PGLongo",
    "platforms": {
        "ios": "8.0",
        "watchos": "2.0"
    },
    "source": {
        "git": "https://github.com/PGLongo/PGLFormatter.git",
        "tag": "3.2.0"
    },
    "source_files": "PGLFormatter/*.swift",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This