自从我在macOS上完成任何工作以来已经过去了大约两年,而自从接触Swift以来已经过去了两年多。我正在对此进行圈子:
我有两个函数,第一个包含一个while循环,用于检查第二个返回的字典中的值。 :
func checkPath(_ strPath:String) {
...
while bIsDesktop == false {
let dictResults:Dictionary<String,Any> = checkPath(arrPath)
if dictResults["isDesktop"] { //-->I'm getting crushed here
//do something with returned array
}
}
private func checkPath(_ arrPath:Array<String>) -> Dictionary<String,Any> {
var bIsDesktop = false
var arrPath = arrPath
if arrPath[0] != "Desktop" {
arrPath.removeFirst()
} else {
bIsDesktop = true
}
return ["paths":arrPath,"isDesktop":bIsDesktop]
}
arrPath是一个字符串数组(文件路径项)
我知道返回的字典就是这样[String:Any]
无论我遇到什么问题,我都会遇到某种编译器错误,例如:
Optional type 'Any?' cannot be used as a boolean; test for '!= nil' instead
在更为宏大的方案中,如果有一个更优雅的解决方案,我想做的就是剥离路径到/// File:User / username / Desktop /用户选择的文件夹。 。,并且只有Desktop / ...如果有比我尝试的解决方案更好的解决方案,我将不胜感激,但是如果您也可以解释为什么我无法从词典中删除可选内容,我也将不胜感激。
我尝试过:
guard let bIsDesktop = dictResults["isDesktop" as Bool else { ...
if dictResults?["isDesktop"]...
和其他各种排列。我得到nil或编译器错误。