如果想要获得类似于Bash -d
if
条件的功能.
我知道如何测试文件是否存在,如果文件存在fileExistsAtPath()
则返回bool"true",如果不存在则返回"false"(假设path
是包含文件路径的字符串):
if NSFileManager.fileExistsAtPath(path) {
print("File exists")
} else {
print("File does not exist")
}
Run Code Online (Sandbox Code Playgroud)
但是,我想检查指定的路径path
是否是目录,类似于以下bash
代码:
if [ -d "$path" ]; then
echo "$path is a directory"
elif [ -f "$path" ]; then
# this is effectively the same as fileExistsAtPath()
echo "$path is a file"
fi
Run Code Online (Sandbox Code Playgroud)
这是可能的,如果可以,应该如何执行?
Xcode 8引入了一种名为'SF Mono'的新字体.
这是我个人觉得非常易读的字体,我想在一个字体中使用它NSTextView
.但是,要为a设置字体NSTextView
,需要使用NSFont
对象.问题是我似乎无法SF Mono
在可用字体列表中找到字体NSFontManager.shared().availableFonts
.
有谁知道为其字体SF Mono
初始化NSFont(NSFont(name: String, size: CGFloat)
)的程序名称是什么?
最近,我一直在尝试使用Swift创建一个(非常)简单的程序,它允许您通过SSH连接到服务器并执行一些文件.不幸的是,我无法弄清楚如何在Swift应用程序中完全启动SSH会话.这是我写的一些代码:
var sshConnectCommand = ["-c", "spawn ssh "+sshUsername+"@"+sshHost+"; expect assword:; send "+sshPassword+"\r"]
func sshIn() {
//starting ssh session
let sshConnect = NSTask()
sshConnect.arguments = [testCmd]
//rerouting output through a pipe
sshConnect.standardOutput = logAppend
//launch!
sshConnect.launch();
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用NSTask尝试运行'expect'命令输入密码和所有内容.我想尝试尽可能避免使用SSH-keygen,因为这旨在用于用户无权访问的服务器.
总结一下:如果没有SSH-keygen,你将如何连接到SSH,同时完全保留在应用程序代码中?
编辑:我还应该添加,在尝试编译时,我收到此错误:
[Swift._SwiftDeferredNSArray fileSystemRepresentation]: unrecognized selector sent to instance 0x600000032500
.我不确定这意味着什么.
我一直在搜索互联网无济于事,同时试图找到Xcode存储其C++标题(.h)文件的答案,例如"stdio.h"等.我正在寻找目录,只是出于好奇.
提前致谢!掠夺
Swift 3引入了我在框架中使用的新的open关键字。
open
这个框架中的类是否要求在该框架open
之外使用初始化程序,或者init函数继承open
该类的声明?
例如:
open class OpenClass {
var A: String
init() { // does this init() function need to be marked open?
A = String()
}
}
Run Code Online (Sandbox Code Playgroud)
附带的问题:开放类中的变量是否OpenClass
继承了其类的开放性质?