gpt4 book ai didi

xcode - 错误 : value of tuple type '(NSString, NSString)' has no member '0'

转载 作者:行者123 更新时间:2023-12-02 11:09:11 28 4
gpt4 key购买 nike

我正在尝试修改一些一年前运行良好的 Swift 源代码;但是自从 Apple 发布了 Swift 2.0 之后,Xcode 拒绝直接运行我现有的代码;坚持要我更新它以使用新的 Swift。谢谢苹果。

那么,任何人都可以解释这个完全愚蠢的错误吗?

value of tuple type '(NSString, NSString)' has no member '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
}

最佳答案

问题是您试图提取不存在的属性 .values来自元组数组(NSString:NSString) .然而,这个真正的错误被提示 .0 所掩盖。元组的成员。

例如。以下产生与您得到的相同的错误:

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)
}

关于xcode - 错误 : value of tuple type '(NSString, NSString)' has no member '0' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34840967/

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