正确使用NSGetExecutablePath [英] Properly used NSGetExecutablePath
本文介绍了正确使用NSGetExecutablePath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在运行时获取我的应用程序的路径.我从C找到了一些旧资料,并将其相应地转换为函数参数类型定义:
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)")
}
它可以运行,但是我需要一个Int8数组,而不是一个字符串.因此,我必须搜索字符链的末尾并将其转换回字符串.在SWIFT中使用此功能的正确方法是什么?
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?
推荐答案
您需要从C字符串创建Swift字符串
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!
这篇关于正确使用NSGetExecutablePath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文