2

Here is protocol I am using,

public protocol PValidationCondition: Equatable {
    associatedtype T
    func isValid(value: T) -> Bool
    func errorMessage() -> String
}

This is model I am using:

struct PValidationElementWithConditions<T: PValidationCondition> {
    let validationElement: PValidationElement
    var conditions: [T] = []
}

But I would like to store the multiple object in array like the following:

var validationElementsWithConditions: [String: PValidationElementWithConditions] = [:]

But I am getting the following error:

"Cannot convert value of type '[String : PValidationElementWithConditions<_>]' to specified type '[String : PValidationElementWithConditions]'"

1
  • you must specify the PValidationElementWithConditions<YourType> type Commented Aug 10, 2018 at 13:41

2 Answers 2

3

This is incorrect

[String: PValidationElementWithConditions]

you have to do

[String: PValidationElementWithConditions<Type>]

where Type conforms to PValidationCondition & Equatable it's clear from the error here that it needs a paramter

PValidationElementWithConditions<_>

//

extension String  : PValidationCondition { 

    public func isValid(value: String) -> Bool {

        return true
    }

    public func errorMessage() -> String {

        return ""
    }

    public typealias T = String 

}

With

var validationElementsWithConditions: [String: PValidationElementWithConditions<String>] = [:]
Sign up to request clarification or add additional context in comments.

6 Comments

This is not working Cannot convert value of type '[AnyHashable : Any]' to specified type '[String : PValidationElementWithConditions<VComparisionCondition>]'
This is working but I want to save Int, String or any other object in it(same dict), what should I do in this case?
you can consider them all Strings with interpolation "\(12.5)"
You cannot use protocols+generics this way. You cannot mix Conditions<String> with Conditions<Int>. You will need to redesign to simplify your types, or you will need to create a boxing struct PValidationElementWithConditions that does not expose its underlying type (and generally also provides no access to it). But I recommend you go back to the problem you're trying to solve and see if you can build it with simpler closures rather than protocols. Protocols are very fragile in Swift, PATs are 100x more fragile.
The key type issue, though, is if you could do this, how would the compiler be able to deal with validationElementsWithConditions.values.map { $0.conditions }? What type would that return?
|
1

Because it's generic, you need to specify the type T in the declaration. For example:

var validationElementsWithConditions: [String: PValidationElementWithConditions<String>] = [:]

3 Comments

but I want to save Int, String or any other object in it, what should I do in this case?
open class ParakhValidator { open weak var delegate: PValidatorDelegate? private var validationElementsWithConditions: [String: PValidationElementWithConditions<>] = [:] open static let shared = ParakhValidator() private init() { } }
this is my class which is singleton

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.