Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Swift 5's Custom String Interpolation in Practice
Search
Bas Broek
February 06, 2020
Programming
720
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swift 5's Custom String Interpolation in Practice
Bas Broek
February 06, 2020
More Decks by Bas Broek
See All by Bas Broek
Roasting Your App's Accessibility
basthomas
0
48
Building an Accessibility Culture, One Step at a Time (Leeds)
basthomas
0
140
Building an Accessibility Culture, One Step at a Time
basthomas
1
120
Building a modern subscription experience on iOS
basthomas
0
220
Not an afterthought: accessibility from start to finish
basthomas
0
170
Accessibility on Apple Platforms: Beyond VoiceOver
basthomas
0
190
No Touch(screen) Required: Voice & Keyboard Accessibility
basthomas
0
190
Dancing with Dinosaurs: Objective-C and Swift Interop
basthomas
0
190
Effective Pull Request Reviews
basthomas
0
450
Other Decks in Programming
See All in Programming
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
240
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
230
スマートグラスで並列バイブコーディング
hyshu
0
270
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
270
ECSアプリログをFireLensでコスト削減しようとしたけど諦めた話 in Fargate×Node.js
akihisaikeda
2
4.2k
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
210
なぜ型を書くのか? TSKaigi2026で改めて考える #tskaigi_smarthr
kajitack
0
180
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
140
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
180
Webフレームワークの ベンチマークについて
yusukebe
0
190
symfony/aiとlaravel/boost
77web
0
110
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
234
18k
GraphQLとの向き合い方2022年版
quramy
50
15k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
150
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
620
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The browser strikes back
jonoalderson
0
1.4k
The SEO identity crisis: Don't let AI make you average
varn
0
510
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1k
A Tale of Four Properties
chriscoyier
163
24k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
450
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Transcript
Using Swift 5's Custom String Interpolation in Practice @basthomas, Copenhagen
Cocoa, 06-02-2020 1
Who am I • Swift Weekly Brief • Contravariance (stickers)
• RayWenderlich tech editor • iOS Platform at XING @basthomas, Copenhagen Cocoa, 06-02-2020 2
Where do we come from? @basthomas, Copenhagen Cocoa, 06-02-2020 3
! @basthomas, Copenhagen Cocoa, 06-02-2020 4
NSString *name = @"Bas"; [NSString stringWithFormat:@"Hello, %@", name]; [NSString stringWithFormat:@"%.2f",
9.0234]; [NSString stringWithFormat: @"Hello %@, how are you doing this %@? %@", @"Bas", @"evening", @"Great!" ]; @basthomas, Copenhagen Cocoa, 06-02-2020 5
✨ @basthomas, Copenhagen Cocoa, 06-02-2020 6
let name = "Bas" "Hello, \(name)" String(format: "%.2f", 9.0234) //
"Hello \(name), how are you doing this \(timeOfDay)?" @basthomas, Copenhagen Cocoa, 06-02-2020 7
It is very good. @basthomas, Copenhagen Cocoa, 06-02-2020 8
Interlude @basthomas, Copenhagen Cocoa, 06-02-2020 9
Swift Evolution @basthomas, Copenhagen Cocoa, 06-02-2020 10
Multiline String Literals (SE-0168) """ Hello, how are you doing
this \(timeOfDay)? """ @basthomas, Copenhagen Cocoa, 06-02-2020 11
String Literal Delimiters (SE-0200) // before "\"Did you put your
name into the Goblet of Fire, Harry?\" he asked calmly." // after #""Harry! Did you put your name in the Goblet of Fire?!""# @basthomas, Copenhagen Cocoa, 06-02-2020 12
And then... @basthomas, Copenhagen Cocoa, 06-02-2020 13
Custom String Interpolation (aka Fix ExpressibleByStringInterpolation) @basthomas, Copenhagen Cocoa, 06-02-2020
14
Localization @basthomas, Copenhagen Cocoa, 06-02-2020 15
let message: LocalizableString = #"The document "\(name)" could not be
saved."# alert.messageText = String(localized: message) @basthomas, Copenhagen Cocoa, 06-02-2020 16
Errors @basthomas, Copenhagen Cocoa, 06-02-2020 17
extension String.StringInterpolation { mutating func appendInterpolation(_ error: Error) { appendLiteral(error.localizedDescription)
} } fatalError( "Something went wrong: \(ConnectionError.invalidRequest)" ) @basthomas, Copenhagen Cocoa, 06-02-2020 18
Number formatting @basthomas, Copenhagen Cocoa, 06-02-2020 19
extension String.StringInterpolation { mutating func appendInterpolation( number: Double, formatter: NumberFormatter
) { appendInterpolation( ifNotNil: formatter.string(from: NSNumber(value: number)) ) } } let priceFormatter = NumberFormatter() priceFormatter.numberStyle = .currency "\(number: 100.1234, formatter: priceFormatter)" @basthomas, Copenhagen Cocoa, 06-02-2020 20
Logging and privacy @basthomas, Copenhagen Cocoa, 06-02-2020 21
extension String.StringInterpolation { mutating func appendInterpolation(private: String) { #if DEBUG
appendLiteral(`private`) #else appendLiteral("@@@") #endif } } let name = "Bas" let password = "Broek" print("User \(name) has password \(private: password)") @basthomas, Copenhagen Cocoa, 06-02-2020 22
Other instances @basthomas, Copenhagen Cocoa, 06-02-2020 23
"\(properties == nil ? "nil" : String(describing: properties ?? [:]))"
@basthomas, Copenhagen Cocoa, 06-02-2020 24
extension String.StringInterpolation { mutating func appendInterpolation( dictionary: Dictionary<AnyHashable, Any>?, default:
@autoclosure () -> String = "nil" ) { if let dictionary = dictionary { appendLiteral(String(describing: dictionary)) } else { appendLiteral(`default`()) } } } "\(dictionary: properties)" @basthomas, Copenhagen Cocoa, 06-02-2020 25
123 > maxBadgeCount ? String("\(maxBadgeCount)+") : String(123) @basthomas, Copenhagen Cocoa,
06-02-2020 26
extension String.StringInterpolation { mutating func appendInterpolation( number: Int, maximum: @autoclosure
() -> Int ) { let max = maximum() if number > max { appendLiteral("\(max)+") } else { appendLiteral("\(number)") } } } "\(number: number, maximum: maxBadgeCount)" @basthomas, Copenhagen Cocoa, 06-02-2020 27
! @basthomas, Copenhagen Cocoa, 06-02-2020 28
Polluting a namespace @basthomas, Copenhagen Cocoa, 06-02-2020 29
Code, documentation, tests @basthomas, Copenhagen Cocoa, 06-02-2020 30
struct BadgeCount: CustomStringConvertible { let value: Int var maximum =
99 var description: String { if value > maximum { return "\(maximum)+" } else { return "\(value)" } } } "\(BadgeCount(value: 123, maximum: 99))" "\(BadgeCount(value: 123))" @basthomas, Copenhagen Cocoa, 06-02-2020 31
Going all in @basthomas, Copenhagen Cocoa, 06-02-2020 32
struct HTMLComponent: ExpressibleByStringLiteral, ExpressibleByStringInterpolation, CustomStringConvertible { struct StringInterpolation: StringInterpolationProtocol {
var output = "" init(literalCapacity: Int, interpolationCount: Int) { output.reserveCapacity(literalCapacity * 2) } mutating func appendLiteral(_ literal: String) { output.append(literal) } mutating func appendInterpolation(twitter: String) { output.append("<a href=\"https://twitter.com/\(twitter)\">@\(twitter)</a>") } mutating func appendInterpolation(email: String) { output.append("<a href=\"mailto:\(email)\">\(email)</a>") } } let description: String init(stringLiteral value: String) { description = "<p>\(value)</p>" } init(stringInterpolation: StringInterpolation) { self.init(stringLiteral: stringInterpolation.output) } } @basthomas, Copenhagen Cocoa, 06-02-2020 33
struct StringInterpolation: StringInterpolationProtocol { var output = "" init(literalCapacity: Int,
interpolationCount: Int) { output.reserveCapacity(literalCapacity * 2) } mutating func appendLiteral(_ literal: String) { output.append(literal) } mutating func appendInterpolation(twitter: String) { output.append("<a href=\"https://twitter.com/\(twitter)\">@\(twitter)</a>") } mutating func appendInterpolation(email: String) { output.append("<a href=\"mailto:\(email)\">\(email)</a>") } } let html: HTMLComponent = "Find me on \(twitter: "basthomas") or send an email to \(email: "
[email protected]
")" // <p>Find me on <a href="https://twitter.com/basthomas">@basthomas</a> // or send an email to <a href="mailto:
[email protected]
">
[email protected]
</a></p> @basthomas, Copenhagen Cocoa, 06-02-2020 34
Optimize code for reading, not writing @basthomas, Copenhagen Cocoa, 06-02-2020
35
Communication is hard @basthomas, Copenhagen Cocoa, 06-02-2020 36
Thanks! @basthomas @basthomas, Copenhagen Cocoa, 06-02-2020 37