带变音符号的Swift String无法正确传入CLI进程问题求助
问题:Swift Process() 传入含变音符号的字符串调用shortcuts CLI报错
该问题已困扰我数月,投入大量时间尝试解决,恳请提供帮助或指导!😅
运行以下Swift函数时,传入含变音符号的字符串(如ä)总会抛出错误。我的最终目标是让任意语言的特殊字符都能正常传入CLI接口。
func runShortcut(inputShortcut: String) { let shortcutsCLI = Process() shortcutsCLI.standardInput = nil //TODO: DTS Fix. This allows us to run the Shortcut!!! shortcutsCLI.executableURL = URL(fileURLWithPath: "/usr/bin/shortcuts") shortcutsCLI.arguments = ["run", inputShortcut.decomposedStringWithCanonicalMapping] //decomposedStringWithCanonicalMapping / composed print("Full args: ", shortcutsCLI.arguments) //Prints: Optional(["run", "ä"]) do { try shortcutsCLI.run() print("Should've ran the shortcut...") } catch { print("\(error)") } } runShortcut(inputShortcut: "ä")
已尝试的解决方案
- 用Unicode形式构造输入字符串(如
let str = "\u{00c4}") - 从二进制构造字符串
- 使用
str.precomposedStringWithCanonicalMapping和decomposedStringWithCanonicalMapping处理字符串
补充信息
- 直接从CLI、Python和Go调用
shortcuts run命令时,含特殊字符的输入一切正常 - 将
inputShortcut保存到本地txt文件,执行cat "/PathToTxt/a.txt" | xargs shortcuts run也能正常运行,说明并非字符串本身的问题。我推测问题出在Swift的Process()传递字符串参数的方式上,但不确定是字符串处理问题还是Process()底层实现的问题。
内容的提问来源于stack exchange,提问作者SENTINELITE
相关产品推荐
相关产品推荐

