Properly used NSGetExecutablePath

不打扰是莪最后的温柔 提交于 2019-12-10 13:57:30

问题


I try to get the path to my application at runtime. I found some old sources from C and converted it accordingly to the functions parameter type definition:

var path = [Int8] (count:1024, repeatedValue: 0)
var bufsize : UInt32 = 1024

if _NSGetExecutablePath(&path, &bufsize) == 0 {
println("executable path is \(path)")
}

It runs, but I need an Int8 array, not a string. So I have to search for the end of the character chain and convert it back to a string. What is the correct way to use this function in SWIFT?


回答1:


You need to create a Swift String from a C String

let executablePath = String(CString: path, encoding: NSASCIIStringEncoding)!
println("executable path is \(executablePath)")

But there is an easier way to get the path to the executable

let executablePath = Bundle.main.executablePath!



回答2:


In Swift 4

let executablePath = Bundle.main.executablePath!
print(executablePath)


来源:https://stackoverflow.com/questions/32090342/properly-used-nsgetexecutablepath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!