字符串扩展提供额外参数错误

时间:2017-02-09 14:13:41

标签: ios swift

我有一个来自这个question的字符串扩展名,看起来非常简单,只是我得到一个额外的参数错误。 的可变

var modelDict: [Int: [String: String]] = [:]

扩展:

import Foundation


extension String {
    func contains(_ string: String, options: String.CompareOptions) -> Bool {
        return range(of: string, options: options) != nil
    }
}

用法:

let filteredDict = self.sharedDataVal.modelDict.contains(manufacturerCodeVar, options: .caseInsensitive)

print("Filtered Array \(filteredDict)")

知道为什么.caseInsensitive被标记为额外的arg?

根据Rob的建议编辑,但我认为我还没有这样做。

变量定义:

class sharedData {

    static let sharedInstance = sharedData()
    struct model{
        var id: String
        var modelName: String
        var modelNumber: String
        var manuShort: String
        var phiTypeCode: String
        var phiTypeDescription: String
    }

    var modelDictTest:          [Int: [model]] = [:]
}

加载词典:

    let modelID              = recordInfo["id"] as? String
    let modelName            = recordInfo["modelname"] as? String
    let modelNumber          = recordInfo["modelnumber"]as? String
    let modelManuShort       = recordInfo["manu_short"]as? String
    let modelPhiTypeCode     = recordInfo["phitypecode"]as? String
    let modelPhiDescription  = recordInfo["phitypedescription"]as? String

    let localModelDict = sharedData.model(id: modelID!, modelName: modelName!, modelNumber: modelNumber!, manuShort: modelManuShort!, phiTypeCode: modelPhiTypeCode!, phiTypeDescription: modelPhiDescription!)

用法:

let filteredDict = self.sharedDataVal.modelDictTest.contains(manufacturerCodeVar, options: .caseInsensitive)

仍然得到相同的错误,但我确信我没有抓住正确加载Struct modelDictTest

1 个答案:

答案 0 :(得分:3)

modelDict的类型为[Int: [String: String]]。您已在String上撰写了附加信息。那是没有关系的。

相反,您从contains(_:)获得标准Sequence,其中没有options参数。

作为一般规则,如果您有类似[Int: [String: String]]的类型并且您发现自己想要为该类型创建扩展,那么您真的打算让model成为具有的结构 [Int: [String: String]]。然后你可以添加方法,而不需要扩展。