且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Xcode更改未修改的故事板和XIB文件

更新时间:2021-09-28 10:01:45

这不是错误,这是Xcode处理故事板文件的结果。
我正在为故事板文件编写差异和合并程序(GitHub链接)我已经花了小时分析故事板文件逻辑以及Xcode如何处理它。这就是我发现的:

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:


  • 为什么故事板文件会发生奇怪的变化?
    Xcode使用NSXML API将故事板文件解析为一些基于 NSSet 的逻辑树结构。当Xcode需要编写更改时,它会根据逻辑树结构创建 NSXMLDocument ,清除storyboard文件并调用 XMLDataWithOptions:再次填写文件。因为集合不保留其元素的顺序,即使最轻微的修改也可能会改变整个故事板XML文件。

  • Why do weird changes occur in storyboard files? Xcode uses the NSXML API to parse storyboard files into some NSSet-based logical tree structure. When Xcode needs to write changes it creates an NSXMLDocument based on the logical tree structure, clears the storyboard file and calls XMLDataWithOptions: 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.

为什么类标记会消失或者随机重新出现?
< class> 部分只不过是一个内部Xcode缓存。 Xcode使用它来缓存有关类的信息。缓存经常更改。当类 .h / .m 文件被打开并在Xcode怀疑它们过时时被删除时添加元素(至少较旧的Xcodes表现得像这样)。保存故事板时,会转储当前版本的缓存,这就是< class> 部分经常更改甚至消失的原因。

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.

我没有反向设计Xcode;我通过试验Xcode和storyboard文件来做出这些观察。尽管如此,我几乎100%确定它是这样工作的。

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.

结论


  • 缓存部分不重要;您可以放心地忽略其中的任何更改。

  • 与您在所有论坛上可以找到的内容相反,合并故事板文件并不是一项复杂的任务。例如,假设您在故事板文档中更改了 MyController1 视图控制器。打开storyboard文件,找到类似这样的内容
    < viewController id =ory-XY-OBMsceneMemberID =MyController1>
    您可以安全地仅提交此部分中的更改并忽略其他所有内容。如果您更改了segues或约束,也可以在里面提交任何ory-XY-OBM的内容。简单!

  • 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!