Coder Social home page Coder Social logo

t-pham / nooptionalinterpolation Goto Github PK

View Code? Open in Web Editor NEW
51.0 3.0 3.0 77 KB

✍️ No "Optional(...)" in string interpolation + Easy pluralization

License: MIT License

Ruby 8.41% Swift 86.92% Objective-C 4.67%
swift optional pluralization plural-form cocoapods podfile cartfile carthage ios osx

nooptionalinterpolation's Introduction

'   ____    ___                                                                             
'  |    \  /   \                                                                            
'  |  _  ||     |                                                                           
'  |  |  ||  O  |                                                                           
'  |  |  ||     |                                                                           
'  |  |  ||     |                                                                           
'  |__|__| \___/                                                                            
'                                                                                           
'    ___   ____  ______  ____  ___   ____    ____  _                                        
'   /   \ |    \|      ||    |/   \ |    \  /    || |                                       
'  |     ||  o  )      | |  ||     ||  _  ||  o  || |                                       
'  |  O  ||   _/|_|  |_| |  ||  O  ||  |  ||     || |___                                    
'  |     ||  |    |  |   |  ||     ||  |  ||  _  ||     |                                   
'  |     ||  |    |  |   |  ||     ||  |  ||  |  ||     |                                   
'   \___/ |__|    |__|  |____|\___/ |__|__||__|__||_____|                                   
'                                                                                           
'   ____  ____   ______    ___  ____   ____   ___   _       ____  ______  ____  ___   ____  
'  |    ||    \ |      |  /  _]|    \ |    \ /   \ | |     /    ||      ||    |/   \ |    \ 
'   |  | |  _  ||      | /  [_ |  D  )|  o  )     || |    |  o  ||      | |  ||     ||  _  |
'   |  | |  |  ||_|  |_||    _]|    / |   _/|  O  || |___ |     ||_|  |_| |  ||  O  ||  |  |
'   |  | |  |  |  |  |  |   [_ |    \ |  |  |     ||     ||  _  |  |  |   |  ||     ||  |  |
'   |  | |  |  |  |  |  |     ||  .  \|  |  |     ||     ||  |  |  |  |   |  ||     ||  |  |
'  |____||__|__|  |__|  |_____||__|\_||__|   \___/ |_____||__|__|  |__|  |____|\___/ |__|__|
'

NoOptionalInterpolation

CI Status GitHub issues Codecov Documentation

GitHub release Platform License

Carthage

CocoaPods CocoaPods downloads

Description

NoOptionalInterpolation gets rid of "Optional(...)" and "nil" in Swift's string interpolation. This is particularly helpful when you set text to UI elements such as UILabel or UIButton. Since XCode currently, as of the time this is written, does not show any warnings when interpolating Optionals, and you might sometimes need to change your variables' type between Optional and non-Optional, this library ensures that the text you set never ever includes that annoying additional "Optional(...)". You can also revert to the default behavior when needed.

Besides, the library makes pluralizing your text easier with custom operators.

Usage

Remove "Optional(...)" and "nil":

Just import NoOptionalInterpolation and everything is done for you.

import NoOptionalInterpolation

let n: Int? = 1
let t: String? = nil
let s: String? = "string1"
let o: String?? = "string2"

let i = "\(n) \(t) \(s) \(o)"
print(i) // 1  string1 string2

Also, please note that this does not affect the print function. Hence, print(o) (as opposed to print("\(o)"), o as in the example above) would still print out Optional(Optional("string2")).

Revert to the default behavior:

Use the * operator for your Optionals.

...
let i = "\(n*) \(t*) \(s*) \(o*)"
print(i) // Optional(1) nil Optional("string1") Optional(Optional("string2"))

Pluralization:

Use the ~ operator to pluralize words.

let age = 42
let text = "I am \(age ~ "year") old" // "I am 42 years old" // actually not // for now

Use the / operator to provide the plural form.

let memberCount = 42
let text = "The team consists of \(memberCount ~ "person" / "people")" // "The team consists of 42 people"

To omit the quantity, swap the position of the quantity and the word.

let listenerCount = 42
let text = "Do it \("yourself" / "yourselves" ~ listenerCount)" // "Do it yourselves"

It also works with Optionals.

let count: Int?? = 42
let fruit: String?? = "apple"
let text = "I have \(count ~ fruit)" // "I have 42 apples"

By default, if you don't provide a plural form using the / operator, an "s" is appended to your word to make the plural form. To make the pluralization smarter, you can specify a custom PluralizerType. You can find one here.

In your Podfile:

pod 'Pluralize.swift', :git => "https://github.com/joshualat/Pluralize.swift.git"

NOTE: Pluralize.swift pod is not yet compatible with Swift 3.

Then:

import NoOptionalInterpolation
import Pluralize_swift

extension Pluralize: NoOptionalInterpolation.Pluralizer {}

...
NoOptionalInterpolation.PluralizerType = Pluralize.self
assert(42 ~ "oasis" == "42 oases")

Installation

Add the line below to your Cartfile:

github "T-Pham/NoOptionalInterpolation"

Add the line below to your Podfile:

pod 'NoOptionalInterpolation'

Add all the files in /NoOptionalInterpolation/Classes/ to your project. You are all set.

Compatibility

From version 3.0.0, Swift 3 syntax is used. If your project is still using earlier versions of Swift, please use a NoOptionalInterpolation version prior to 3.0.0.

Podfile

pod 'NoOptionalInterpolation', '~> 2.0.6'

or Cartfile

github "T-Pham/NoOptionalInterpolation" ~> 2.0.6

License

NoOptionalInterpolation is available under the MIT license. See the LICENSE file for more info.

nooptionalinterpolation's People

Contributors

t-pham avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nooptionalinterpolation's Issues

Ambiguous use of 'init(string InterpolationSegment:)'

Im getting this error for some odd reason now. Any ideas what it could be thanks?
screen shot 2017-05-08 at 12 11 19 am

I have tried deleting my pod files, reinstalling Cocoapods, cleaning the pod cache, and cleaning the build folders, but nothing has done the trick.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.