JLanders
JLanders

Reputation: 101

String extension giving extra argument error

I have a string extension from this question that seems pretty straightforward only I am getting an extra argument error. Variable

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

Extension:

import Foundation


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

Usage:

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

print("Filtered Array \(filteredDict)")

Any idea why .caseInsensitive is flagged as an extra arg?

Edited based on Rob's suggestion, but I don't think I am getting it yet.

Variable Def:

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]] = [:]
}

Loading the Dictionary:

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

Usage:

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

Still getting the same error, but I am sure I am not grasping loading a Struct int modelDictTest correctly

Upvotes: 2

Views: 90

Answers (1)

Rob Napier
Rob Napier

Reputation: 299355

modelDict is of type [Int: [String: String]]. You've written an extension on String. That's not related.

Instead, you're getting the standard contains(_:) from Sequence, which has no options parameter.

As a general rule, if you have a type like [Int: [String: String]] and you find yourself wanting to create extensions for that type, you really meant to make model a struct that has an [Int: [String: String]]. Then you can just add methods to it, and don't need extensions.

Upvotes: 3

Related Questions