错误:元组类型的值'(NSString,NSString)'没有会员' 0'

时间:2016-01-17 16:58:05

标签: xcode swift compiler-errors tuples

我试图修改一些一年前工作得很好的Swift源码;但自Apple发布Swift 2.0以来,Xcode拒绝使用我现有代码的空白;坚持要我更新它以使用新的Swift。谢谢Apple。

那么,有人能解释这个完全愚蠢的错误吗?

  

元组类型的值'(NSString,NSString)'没有会员' 0'

失败的代码行:

if let match = (try Filesystem.GetMounts()?.filter { 
        mapping["pattern"] != nil ? try $0.0 =~ mapping["pattern"]! : $0.0 as String == uncpath! 
    }.values.array.first) {
    ...
}

GetMounts函数签名:

class func GetMounts() throws -> [NSString:NSString]?

正则表达式运算符:

func =~(string:NSString, regex:NSRegularExpression) -> Bool {
    let matches = regex.numberOfMatchesInString(string as String, options: [], range: NSMakeRange(0, string.length))
    return matches > 0
}
func =~(string:NSString, pattern:NSString) throws -> Bool {
    let matches = try NSRegularExpression(pattern: pattern as String, options: NSRegularExpressionOptions.DotMatchesLineSeparators).numberOfMatchesInString(string as String, options: [], range: NSMakeRange(0, string.length))
    return matches > 0
}
func =~(string:NSString, pattern:String) throws -> Bool {
    let matches = try NSRegularExpression(pattern: pattern as String, options: NSRegularExpressionOptions.DotMatchesLineSeparators).numberOfMatchesInString(string as String, options: [], range: NSMakeRange(0, string.length))
    return matches > 0
}

1 个答案:

答案 0 :(得分:1)

问题在于您尝试从元组.values数组中提取不存在的属性(NSString:NSString)。然而,抱怨元组的.0成员会掩盖这个真正的错误。

E.g。以下结果会产生相同的错误:

let a: [NSString:NSString] = ["Hello":"World", "foo":"bar"]

let opt : Int? = 1
if let b = (a.filter() {
    opt != nil ? $0.0 == "Hello" : $0.0 as String == "foo"
    }.values.array.first) { // <-- actual error
        // ...
}

而以下情况则不然:

let a: [NSString:NSString] = ["Hello":"World", "foo":"bar"]

let opt : Int? = 1
if let b = (a.filter() {
    opt != nil ? $0.0 == "Hello" : $0.0 as String == "foo"
    }.first) {
        print(b) // (Hello, World)
}