Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

T-Pham/NoOptionalInterpolation

Repository files navigation

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

CI StatusGitHub issuesCodecovDocumentation

GitHub releasePlatformLicense

Carthage

CocoaPodsCocoaPods 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 asUILabel orUIButton. Since XCode currently, as of the time this is written, does not show any warnings when interpolatingOptionals, and you might sometimes need to change your variables' type betweenOptional 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 NoOptionalInterpolationletn:Int?=1lett:String?=nillets:String?="string1"leto:String??="string2"leti="\(n)\(t)\(s)\(o)"print(i) // 1  string1 string2

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

Revert to the default behavior:

Use the* operator for yourOptionals.

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

Pluralization:

Use the~ operator to pluralize words.

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

Use the/ operator to provide the plural form.

letmemberCount=42lettext="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.

letlistenerCount=42lettext="Do it\("yourself"/"yourselves"~ listenerCount)" // "Do it yourselves"

It also works withOptionals.

letcount:Int??=42letfruit:String??="apple"lettext="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 customPluralizerType. You can find onehere.

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 NoOptionalInterpolationimport Pluralize_swiftextensionPluralize:NoOptionalInterpolation.Pluralizer{}...NoOptionalInterpolation.PluralizerType=Pluralize.selfassert(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 theLICENSE file for more info.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2026 Movatter.jp