diff --git a/public/_data.json b/public/_data.json index c28ea10..8b036f8 100644 --- a/public/_data.json +++ b/public/_data.json @@ -19,6 +19,17 @@ "title": "Curriculum vitae" }, "posts": [ + { + "title": "A nil-coalescing alternative for Swift", + "date": "6th October, 2017", + "timestamp": 1507324813, + "tags": [ + "ios", + "swift" + ], + "author": "Sami J. Samhuri", + "url": "/posts/2017/10/swift-optional-or" + }, { "title": "Easy Optimization Wins", "date": "10th August, 2016", @@ -116,17 +127,6 @@ "author": "Sami J. Samhuri", "url": "/posts/2015/07/scripts-to-rule-them-all", "link": "http://githubengineering.com/scripts-to-rule-them-all/" - }, - { - "title": "→ Debugging Layouts with Recursive View Descriptions in Xcode", - "date": "2nd June, 2015", - "timestamp": 1433288135, - "tags": [ - - ], - "author": "Sami J. Samhuri", - "url": "/posts/2015/06/debugging-layouts-with-recursive-view-descriptions-in-xcode", - "link": "http://jeffreysambells.com/2013/01/24/debugging-layouts-with-recursive-view-descriptions-in-xcode" } ] } \ No newline at end of file diff --git a/public/posts/2017/10/_data.json b/public/posts/2017/10/_data.json new file mode 100644 index 0000000..510abce --- /dev/null +++ b/public/posts/2017/10/_data.json @@ -0,0 +1,15 @@ +{ + "swift-optional-or": { + "id": "swift-optional-or", + "author": "Sami J. Samhuri", + "title": "A nil-coalescing alternative for Swift", + "date": "6th October, 2017", + "timestamp": 1507324813, + "link": null, + "url": "/posts/2017/10/swift-optional-or", + "tags": [ + "ios", + "swift" + ] + } +} diff --git a/public/posts/2017/10/index.ejs b/public/posts/2017/10/index.ejs new file mode 100644 index 0000000..72c8d6b --- /dev/null +++ b/public/posts/2017/10/index.ejs @@ -0,0 +1 @@ +<%- partial('../../_month') %> diff --git a/public/posts/2017/10/swift-optional-or.md b/public/posts/2017/10/swift-optional-or.md new file mode 100644 index 0000000..5f0471f --- /dev/null +++ b/public/posts/2017/10/swift-optional-or.md @@ -0,0 +1,28 @@ +Swift compile times leave something to be desired and a common culprit is the affectionately-named [nil-coalescing operator][nilop]. A small extension to `Optional` can improve this without sacrificing a lot of readability. + +```Swift +extension Optional { + func or(_ defaultValue: Wrapped) -> Wrapped { + switch self { + case .none: return defaultValue + case let .some(value): return value + } + } +} +``` + +And you use it like so: + +```Swift +let dict: [String : String] = [:] +let maybeString = dict["not here"] +print("the string is: \(maybeString.or("default"))") +let otherString = dict["not here"].or("something else") +``` + +I'm sure someone else has come up with this already but I haven't seen it yet. + +_([gist available here][gist])_ + +[nilop]: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html#//apple_ref/doc/uid/TP40014097-CH6-ID72 +[gist]: https://gist.github.com/samsonjs/c8933c07ad985b74aba994f2fdab8b47 diff --git a/public/posts/2017/index.ejs b/public/posts/2017/index.ejs new file mode 100644 index 0000000..74a1f27 --- /dev/null +++ b/public/posts/2017/index.ejs @@ -0,0 +1 @@ +<%- partial('../_year') %>