一个 Swift 包,为 Xcode 中的字符串文件提供解析器

字符串解析器

概述

这个 swift 包提供了一种简单的方法来增强您的 Xcode 构建,以便可以在构建时使用实际错误消息验证项目中的任何字符串文件。

Xcode 所做的只是(从 Xcode 14.1 开始)在构建时告诉你你的字符串文件不好,仅此而已。它不会告诉你为什么它不好;只是这不好。

😐 那是没用的。

Xcode 从未为 String 文件提供足够的错误报告,虽然这个功能对 Xcode 来说似乎是一个明显的增值,但这么多年过去了,它还没有完成。

我在5747387年2017月提出了一个反馈项目(FB<>),希望在这个问题上得到一些关注。苹果的快速反应是,该问题将作为重复项关闭。

2018年2018月,我再次询问是否有可能解决这个问题。<>年<>月的回应是,该问题“仍在调查中”。

好吧,现在是 2022 年 <> 月,我厌倦了等待。

解决方案

Xcode不再具有很强的可扩展性。源代码编辑器扩展很弱,对所需的内容毫无用处。能够添加可以在构建阶段使用的插件或扩展实际上很有用。

因此,在社区的一点推动下,我所做的是创建一个小型 swift 脚本来解析命名的字符串文件,输出到标准输出,任何错误消息,以便 Xcode 可以自动拾取并在问题导航器中显示它们。

单击错误将有助于突出显示问题所在的行。

如何使用它

安装

不幸的是,你不能只是把它安装到Xcode中并欣赏风景。你需要将 Swift 文件“StringsParser.swift”放在 Xcode 可以看到它的地方。从技术上讲,它不需要添加到您的项目中,但如果需要,您可以这样做(正如我在此示例项目中所做的那样)。 这个存储库最初只是一个带有解析器工作原型的 Xcode 项目。此存储库是一个 Swift 包,因此您应该能够将包添加到您的项目中。完成此操作后,您应该能够找到StringsParser.swift。

使用它

现在,分析器已位于所需位置,请添加“运行脚本”生成阶段,并将以下内容添加到该阶段:

xcrun --sdk macosx swiftc -parse-as-library Scripts/StringsParser.swift -o CompiledScript
./CompiledScript $SCRIPT_INPUT_FILE_0

As you can see the run script is expecting to find the parser within a folder (or Group) inside the project called “Scripts”. You will need to modify this as appropriate. This script compiles the parser, and then executes it, passing the first script input as provided in the Run Script parameters.

That’s it! Once you have this in place, you’ll get errors reported whenever you have a typo or similar somewhere inside your Strings file, and you’ll be told where it is. Magic!

TODO

  • Add support for a list of files instead of just parsing one.
  • Find a way to get Xcode to display the link between an error and the localization key it actually belongs to. The script already generates notes for this purpose however Xcode refuses to treat them in the same way it does for errors on objective-c code.

GitHub

点击跳转