Skip to content

Commit 3b0d641

Browse files
committed
Add tip #63
1 parent 0c4377c commit 3b0d641

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Here's list of Swift tips & tricks with all additional sources (playgrounds, ima
88

99
## 📃 Table of contents
1010

11-
[#63 `StoryboardIdentifiable` protocol]()<br />
11+
[#63 How to make `UIStoryboard` usage safer?]()<br />
1212
[#62 "Massive" Storyboard]()<br />
1313
[#61 `XCTUnwrap` assertion function]()<br />
1414
[#60 `UITableViewCell` identifier]()<br />
@@ -72,9 +72,48 @@ Here's list of Swift tips & tricks with all additional sources (playgrounds, ima
7272
[#2 Easy way to hide Status Bar](https://github.com/Luur/SwiftTips#2-easy-way-to-hide-status-bar)<br />
7373
[#1 Safe way to return element at specified index](https://github.com/Luur/SwiftTips#1-safe-way-to-return-element-at-specified-index)<br />
7474

75-
## [#63 `StoryboardIdentifiable` protocol]()
75+
## [#63 How to make `UIStoryboard` usage safer?]()
7676

77-
https://medium.com/swift-programming/uistoryboard-safer-with-enums-protocol-extensions-and-generics-7aad3883b44d
77+
Below I want to show you a few simple ways how to make usage of string literals connected with `UIStoryboard` much more safer.
78+
79+
* Global constant string literals
80+
At first it sounds like a good idea. You only need to define the constant once and it will be available everywhere. Then if you want to change its value, there is only one place in code to change for which the effects will cascade throughout the project.
81+
But global constants have some disadvantages. Not as much as global variables but still enought to stop using them. It's not the best practice. I will cover the topic of andvatages and disadavntages of global constants/variables usage in my next posts.
82+
83+
* Relatable storyboard names
84+
Your storyboards should be named after the sections of which they cover. It's a general rule. If you have a storyboard which houses view controllers are related to Profile, then the name of that storyboard’s file should be `Profile.storyboard`.
85+
86+
* Uniform storyboard identifiers
87+
When you want to use `Storyboard Identifiers` on your view controllers, usage of the class names as identifiers will be a good practice. For example, “ProfileViewController” would be the identifier for ProfuleViewController. Adding this to your naming convention is a good idea.
88+
89+
* Enum
90+
Try to consider enums as uniform, centralized global string literal identifiers for 'UIStoryboard'. You can create UIStoryboard class extension which defines all the storyboard files you have in your project. You can also add the convenience initializer or class function to add more syntactic sugar.
91+
92+
```swift
93+
extension UIStoryboard {
94+
enum Storyboard: String {
95+
case profile
96+
case login
97+
98+
var name: String {
99+
return rawValue.capitalized
100+
}
101+
}
102+
103+
convenience init(storyboard: Storyboard, bundle: Bundle? = nil) {
104+
self.init(name: storyboard.name, bundle: bundle)
105+
}
106+
// let storyboard = UIStoryboard(storyboard: .profile)
107+
108+
class func storyboard(storyboard: Storyboard, bundle: Bundle? = nil) -> UIStoryboard {
109+
return UIStoryboard(name: storyboard.name, bundle: bundle)
110+
}
111+
// let storyboard = UIStoryboard.storyboard(.profile)
112+
}
113+
```
114+
I hope provided solutions will really help you to maintain your storyboards.
115+
116+
Back to [Top](https://github.com/Luur/SwiftTips#-table-of-contents)
78117

79118
## [#62 "Massive" Storyboard]()
80119

0 commit comments

Comments
 (0)