I'm working on a Swift class that utilizes regex for pattern matching, but I'm encountering an error when trying to initialize the Regex type in my custom class.
class code:
import Foundation
class Challenge {
let title: String
let description: String
let regex: Regex<AnyRegexOutput>
var isComplete: Bool
init(title: String, description: String, regex: Regex<AnyRegexOutput>, isComplete: Bool = false) {
self.title = title
self.description = description
self.regex = regex
self.isComplete = isComplete
}
}
Code with the Error
I am trying to create an array of Challenge instances like this:
var dailyChallenges = [
Challenge(title: "Update Title",
description: "set a new website title",
regex: /<title>(?!webview<\/title>)(.*?)<\/title>/),
Challenge(title: "Add Image",
description: "add an image with a source URL",
regex: /<img(\s.*\s|\s)(src="http.+?")/),
]
The Error Message
However, I am receiving the following error:
Cannot convert value of type 'Regex<(Substring, Substring)>' to expected argument type 'Regex<AnyRegexOutput>'
Arguments to generic parameter 'Output' ('(Substring, Substring)' and 'AnyRegexOutput') are expected to be equal
and :
Cannot convert value of type 'Regex<(Substring, Substring, Substring)>' to expected argument type 'Regex<AnyRegexOutput>'
Arguments to generic parameter 'Output' ('(Substring, Substring, Substring)' and 'AnyRegexOutput') are expected to be equal
Question
How can I modify my class to use regex patterns that can match any number of substrings, so I can avoid this conversion issue?