This is not a bug, this is a consequence of how Xcode processes storyboard files.
I am writing a diff and merge program for storyboard files (GitHub link) and I have spent hours analyzing the storyboard files logic and how Xcode processes it. This is what I discovered:
-
Why do weird changes occur in storyboard files?
Xcode uses the NSXML API to parse storyboard files into someNSSet
-based logical tree structure. When Xcode needs to write changes it creates anNSXMLDocument
based on the logical tree structure, clears the storyboard file and callsXMLDataWithOptions:
to fill the file again. Because sets do not preserve the order of their elements, even the slightest modification could change the whole storyboard XML file. -
Why does the class tag disappear or reappear randomly?
The<class>
section is nothing more than an internal Xcode cache. Xcode use it to cache information about classes. The cache changes often. Elements are added when class.h/.m
files are opened and removed when Xcode suspects they are outdated (at least older Xcodes behave like this). When you save the storyboard, the current version of the cache is dumped, which is why the<class>
section often changes or even disappears.
I have not reverse-engineered Xcode; I made these observations by experimenting with Xcode and storyboard files. Nevertheless, I am almost 100% sure it works this way.
Conclusions:
- Cache section is unimportant; you can safely ignore any change in it.
- Contrary to what you can find on all forums, merging storyboards files is not a complicated task. For example, let’s assume you changed
MyController1
view controller in a storyboard document. Open the storyboard file, and find something like this
<viewController id=”ory-XY-OBM” sceneMemberID=”MyController1”>
.
You can safely commit only changes in this section and ignore everything else. If you changed segues or constraints, also commit anything that has“ory-XY-OBM”
inside. Simple!