我試過我的第一個Swift程序。它意味着從具有參數和裝載服務器卷的shell腳本運行。當我從卷名(例如å,ä,ö)中帶有瑞典字符參數的Xcode啓動程序時,該程序按照原樣運行。如果我從卷名中帶有瑞典語字符參數的命令行啓動程序,掛載失敗,它會在「/卷」中以正確的名稱在掛載點創建一個文件夾。爲什麼不使用NetFSMountURLSync命令行參數中的瑞典語字符?
爲什麼瑞典字符的參數不能在命令行中運行?
代碼:
import Foundation
import NetFS
func mountShare(serverAddress: String, shareName: String, userName: String, password: String) {
let fm = FileManager.default
let mountPoint = "/Volumes/".appendingFormat(shareName)
var isDir : ObjCBool = false
if fm.fileExists(atPath: mountPoint, isDirectory: &isDir) {
if isDir.boolValue {
unmount(mountPoint, 0)
print("Unmounted: \(mountPoint)")
}
}
let sharePathRaw = "\(serverAddress)/\(shareName)"
let sharePathWithPercentEscapes = sharePathRaw.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let sharePath = NSURL(string: sharePathWithPercentEscapes!)
let kNAUIOptionKey = "UIOption"
let kNAUIOptionNoUI = "NoUI"
let mount_options = NSMutableDictionary()
mount_options[kNAUIOptionKey] = kNAUIOptionNoUI
NetFSMountURLSync(sharePath as CFURL!, nil, userName as CFString!, password as CFString!, mount_options, nil, nil)
}
let argCount = CommandLine.argc
if argCount == 5 {
let serverUrl = CommandLine.arguments[1]
let shareName = CommandLine.arguments[2]
let userName = CommandLine.arguments[3]
let password = CommandLine.arguments[4]
mountShare(serverAddress: "\(serverUrl)", shareName: "\(shareName)", userName: "\(userName)", password: "\(password)")
} else {
print("Wrong number of arguments.")
}
實例開始的命令行程序,它的工作原理:
mountVolume "afp://my.server.com" "myVolume" "user" "password"
實例啓動程序的命令行,不是作品:
mountVolume "afp://my.server.com" "myVolumeÅÄÖ" "user" "password"
更多信息: 當我嘗試打印出比較值的所有變量時,一個來自Xcode的程序替代命令行並沒有什麼區別。但是,NetFSMountURLSync在命令行上以代碼2退出而不是0退出。
從命令行運行時,「shareName」的實際值是多少? – rmaddy
我有一個名爲「myVolumeÅÄÖ」的測試卷。 –
這不是我問的。當你實際運行沒有Xcode的程序時,'shareName'變量的實際值是多少?根據您在答案下發布的評論,看起來您的問題是由可選值造成的。 – rmaddy