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

Overview
'   ____    ___                                                                             
'  |    \  /   \                                                                            
'  |  _  ||     |                                                                           
'  |  |  ||  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

Carthage

Add the line below to your Cartfile:

github "T-Pham/NoOptionalInterpolation"

CocoaPods

Add the line below to your Podfile:

pod 'NoOptionalInterpolation'

Manually

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.

You might also like...
A Cross-Platform String and Regular Expression Library written in Swift.

Guitar 🎸 A Cross-Platform String and Regular Expression Library written in Swift. About This library seeks to add common string manipulation function

Swift emoji string parsing library
Swift emoji string parsing library

Croc is a library for parsing emojis on iOS. It provides a simple and lightweight interface for detecting, generating, categorizing and managing emoji

An easy way to add mentions to uitextview like Facebook and Instagram
An easy way to add mentions to uitextview like Facebook and Instagram

OEMentions An easy way to add mentions to uitextview like Facebook and Instagram. It also include a tableview to show the users list to choose from. T

Write amazing, strong-typed and easy-to-read NSPredicate.

PredicateFlow Write amazing, strong-typed and easy-to-read NSPredicate. This library allows you to write flowable NSPredicate, without guessing attrib

A TextView that provides easy to use tagging feature for Mention or Hashtag
A TextView that provides easy to use tagging feature for Mention or Hashtag

Tagging A TextView that provides easy to use tagging feature for Mention or Hashtag. Introduction Tagging is a UIView that encloses a TextView that co

Acčento is an easy-to-use tool for adding Czech diacritics to copied text.
Acčento is an easy-to-use tool for adding Czech diacritics to copied text.

Acčento is an easy-to-use tool for adding Czech diacritics to copied text. The app lives in the menu bar and is activated using a global hot key.

NoOptionalInterpolation gets rid of "Optional(...)" and "nil" in Swift's string interpolation

NoOptionalInterpolation gets rid of "Optional(...)" and "nil" in Swift's string interpolation

UISlider clone with multiple thumbs and values, range highlight, optional snap intervals, optional value labels, either vertical or horizontal.
UISlider clone with multiple thumbs and values, range highlight, optional snap intervals, optional value labels, either vertical or horizontal.

MultiSlider UISlider clone with multiple thumbs and values, range highlight, optional snap intervals, optional value labels, either vertical or horizo

UISlider clone with multiple thumbs and values, range highlight, optional snap intervals, optional value labels, either vertical or horizontal.
UISlider clone with multiple thumbs and values, range highlight, optional snap intervals, optional value labels, either vertical or horizontal.

MultiSlider UISlider clone with multiple thumbs and values, range highlight, optional snap intervals, optional value labels, either vertical or horizo

StyledTextKit is a declarative attributed string library for fast rendering and easy string building.
StyledTextKit is a declarative attributed string library for fast rendering and easy string building.

StyledTextKit is a declarative attributed string library for fast rendering and easy string building. It serves as a simple replacement to NSAttribute

Swift interpolation for gesture-driven animations
Swift interpolation for gesture-driven animations

Interpolate Interpolate is a powerful Swift interpolation framework for creating interactive gesture-driven animations. Usage The 🔑 idea of Interpola

Simple, extensible interpolation framework
Simple, extensible interpolation framework

THIS PROJECT IS NO LONGER MAINTAINED. Popsicle is a Swift framework for creating and managing interpolations of different value types with built-in UI

Swift String Validator. Simple lib for ios to validate string and UITextFields text for some criterias

Swift String validator About Library for easy and fastest string validation based on сciterias. Instalation KKStringValidator is available through Coc

KIEM - Open Source Library, converting mistyped Korean string into English string

KIEM Open Source Library, converting misspelled Korean string into English strin

🏞 A simple iOS photo and video browser with optional grid view, captions and selections written in Swift5.0
🏞 A simple iOS photo and video browser with optional grid view, captions and selections written in Swift5.0

Introduction 🏞 MediaBrowser can display one or more images or videos by providing either UIImage objects, PHAsset objects, or URLs to library assets,

A library that provides undo semantics for the Composable Architecture with optional bridging tofUndoManager.

Swift Composable Undo A library that provides undo semantics for the Composable Architecture with optional bridging with UndoManager. Motivation It is

An optional TextField Replacement for SwiftUI
An optional TextField Replacement for SwiftUI

TextInputView A TextField Replacement In his excellent YouTube tutorial series on Building SwiftUI Components, Peter Friese (@peterfriese on Twitter)

Swift µframework with extensions for the Optional Type

OptionalExtensions Why? Swift's Optional is pretty awesome, but it can always get better. This repository is an humble attempt to add some utility met

Complete Animated GIF Support for iOS, with Functions, NSJSONSerialization-style Class, and (Optional) UIImage Swizzling
Complete Animated GIF Support for iOS, with Functions, NSJSONSerialization-style Class, and (Optional) UIImage Swizzling

AnimatedGIFImageSerialization This library is no longer maintained. In iOS 13+ and macOS 10.15+, use CGAnimateImageAtURLWithBlock instead. AnimatedGIF

Comments
  • Ambiguous use of 'init(string InterpolationSegment:)'

    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.

    opened by whittenator 3
Owner
Thanh Pham
👨‍💻 iOS & Roku
Thanh Pham
Easy string decoration with styles

StyleDecorator Design string simply by linking attributes. Example Create Decorator with specific Style and link it at the end of needed string or wra

Dmytro Pylypenko 15 Nov 4, 2021
Easy Attributed String Creator

The main idea of this project is to have an online tool to be able to visually add formatting to a text and get back a swift and/or objective-c code t

Andres Canal 283 Dec 27, 2022
BonMot is a Swift attributed string library

BonMot (pronounced Bon Mo, French for good word) is a Swift attributed string library. It abstracts away the complexities of the iOS, macOS, tvOS, and

Rightpoint 3.4k Dec 30, 2022
Croc is a swift emoji string parsing library

Croc is a library for parsing emojis on iOS. It provides a simple and lightweight interface for detecting, generating, categorizing and managing emoji characters, making emoji-powered features an easy task for developers.

Joe Kalash 127 Nov 20, 2022
Great Swift String Pluralize Extension

Pluralize.swift Great Swift String Pluralize Extension case-insensitive tons of rules for irregular nouns (plural form) supports uncountable nouns all

Joshua Arvin Lat 193 Nov 8, 2022
👩‍🎨 Elegant Attributed String composition in Swift sauce

Elegant Attributed String composition in Swift sauce SwiftRichString is a lightweight library which allows to create and manipulate attributed strings

Daniele Margutti 2.9k Jan 5, 2023
A comprehensive, lightweight string extension for Swift

SwiftString SwiftString is a lightweight string extension for Swift. This library was motivated by having to search StackOverflow for common string op

Andrew Mayne 1.6k Dec 30, 2022
More powerful label, attributed string builder and text parser.

DDText More powerful label, attributed string builder and text parser. DDLabel More powerful label than UILabel, using TextKit. It supports features b

Daniel 16 Nov 8, 2022
String (and more) validation for iOS

Swift Validators ?? String validation for iOS. Contents Installation Walkthrough Usage Available validators License ReactiveSwift + SwiftValidators Wa

George Kaimakas 241 Nov 13, 2022
🌍⏩📄 Convert ISO8859 1-16 Encoded Text to String in Swift. Supports iOS, tvOS, watchOS and macOS.

ISO8859 Convert ISO8859 1-16 Encoded Text to String in Swift. Usage let encoding = ISO8859.part1 let string = String([...], iso8859Encoding: encoding)

Devran Cosmo Uenal 18 Jan 2, 2023