Storyboard problems and How Tool By Square (codegenutils) Solves Some
Storyboards are great to use, save time and allows developers to visualise the whole (or part) of app, in terms of screen relationships.
####Problem: Storyboards fail at runtime, note compile time
Using storyboards means also accessing storyboards created items programmatically. Such as:
- Segue identifiers.
- Cell reuse identifiers.
- View Controllers identifiers.
When changing an identifier in storyboard, developer should also change the identifier in the code.
This is prone to error.
####Solution: Auto Generated Constants from Storyboard
Square have recently wrote in Square Engineering Blog about a new tool they have released: objc-codegenutils. and specifically: objc-identifierconstants
.
This “generates string identifiers into-compiler checked constants”. AKA a file for every storyboard that contains:
NSString *const <StoryBoardFileName>MySegueIdentifier
or:
NSString *const <StoryBoardFileName>MyCellIdentifier
.
Using constants avoid typos.
####How:
Clone the project at objc-codegenutils.
Open the cloned project.
Choose
identifierconstants
as target:
Build and find the built product:
Copy
objc-identifierconstants
into your project. (I place under<ProjectRoot>/objc-codegen-utils/
).Add it as a run script on every build:
objc-codegen-utils/objc-identifierconstants -o ${SRCROOT}/Classes/GeneratedIdentifiers/ ${SRCROOT}/Classes/en.lproj/MainStoryboard.storyboard
I output the files into ${SRCROOT}/Classes/GeneratedIdentifiers/
and ${SRCROOT}/Classes/en.lproj/MainStoryboard.storyboard
is the path to my storyboard file.
7. Build your project and add the generated files to the project.
8. Replace string identifiers with new constants.
No more typos.