gpt4 book ai didi

swift - 为什么使用 NetFSMountURLSync 的命令行参数中的瑞典语字符不起作用?

转载 作者:行者123 更新时间:2023-11-28 08:24:33 26 4
gpt4 key购买 nike

我尝试了我的第一个 Swift 程序。它旨在从带有参数和安装服务器卷的 shell 脚本运行。当我从 Xcode 启动程序时,在卷名称(例如,å、ä、ö)中使用带有瑞典字符的参数,该程序可以正常工作。如果我从命令行启动程序,并在卷名称中使用带有瑞典字符的参数,则安装失败,它会在安装点创建一个文件夹,并在“/Volumes”中使用正确的名称。

为什么带有瑞典字符的参数在命令行中不起作用?

代码:

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 退出。

最佳答案

如果在将连接字符串传递给 NSURL 的 init 之前首先转义连接字符串相关部分中的所有特殊字符,是否可行?

let encodedShareName = shareName.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
let sharePathRaw = "\(serverAddress)/\(encodedShareName!)"

关于swift - 为什么使用 NetFSMountURLSync 的命令行参数中的瑞典语字符不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452115/

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