gpt4 book ai didi

xcode - 从编译后的代码中找到 Swift 中的源项目目录

转载 作者:可可西里 更新时间:2023-11-01 01:06:49 27 4
gpt4 key购买 nike

我想在我自己的 swift 项目中找到我未编译的源代码的目录(因为我想计算它的行​​数)。

let projectRepo = "/Users/tombrown/Workspace/SwiftGolf" // <-- I want this programmatically
let path = "\(projectRepo)/SwiftGolf/Golf.swift"

var fileContents = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil)

我用 NSBundle.mainBundle().bundlePath 找到了编译后的位置,但它不包含未编译的代码。有什么想法吗?也许有一种方法可以在构建过程中写入一个临时文件?

最佳答案

挑战在于如何快速推断运行时与编译时的位置。 xcode 建议中的参数效果很好,可以作为解决方案接受。我无法使用这种方法,因为我使用的是没有 xcode 的 SPM。另一种方法是使用这个库 https://github.com/JohnSundell/Files

在你的 Package.swift 中

dependencies: [
.Package(url: "https://github.com/JohnSundell/Files.git", Version(1,8,0))
]

然后在你的代码中

import Files
let projectRepo = "\(Folder.home.path)/gitWorkspace/SwiftGolf"
do {
let model = try Data(contentsOf:URL(fileURLWithPath: "\(projectDir)/\(modelFile)"))

}catch {

print("error: \(error)")

}

另一个选项是 CommandLineKit/xcode arguments

import CommandLineKit .  
let cmdLine = CommandLineKit.CommandLine() .
let file = StringOption(shortFlag: "f",
longFlag: "file",
required: true,
helpMessage: "The file you're trying to parse") .
cmdLine.addOptions(file)


do {
try cmdLine.parse()
print(file.value!)
} catch {
print("error: \(error)")
exit(-1)
}

enter image description here

关于xcode - 从编译后的代码中找到 Swift 中的源项目目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24946085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com