Last active
November 4, 2024 07:15
-
-
Save christianselig/6ccb9b1538012aa0438689481e5fa960 to your computer and use it in GitHub Desktop.
Revisions
-
christianselig revised this gist
Apr 14, 2023 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,11 @@ // Before π let doingSomethingToOptionalView1: CGFloat? = { guard let firstSubview = subviews.first else { return nil } return (firstSubview.bounds.width / 2.0) + otherView.bounds.width }() // After π let doingSomethingToOptionalView2 = subviews.first.useIfNotNil { ($0.bounds.width / 2.0) + otherView.bounds.width } // βοΈ Internals -
christianselig created this gist
Apr 14, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ // Before π let firstSubviewWidth1: CGFloat? = { guard let firstSubview = subviews.first else { return nil } return firstSubview.bounds.width }() // After π let firstSubviewWidth2 = subviews.first.useIfNotNil { $0.bounds.width } // βοΈ Internals extension Optional { /// If the value is not nil, transform it in the specified block. If it is nil, simply returns nil back. Functionally identical to the `map` function on `Optional`, but better named. func useIfNotNil<U>(_ transform: (Wrapped) throws -> U) rethrows -> U? { switch self { case .some(let value): return .some(try transform(value)) case .none: return .none } } }