I'm writing a function that given an array of objects that contain a property called url, remove all objects with bad urls.
Here's what I have:
func cleanArray(data:[String: Any])->Void {
let uris = data.filter{($0["url"] as! String).range(of: #"^(https?|file|ftp)"#, options: .regularExpression) != nil };
}
But xcode is showing an error in $0:
Value of tuple type '(key: String, value: Any)' has no member 'subscript'
String. You should extract the data from your dictionary into structs, and perform this validation in the initializer of your struct. Your code doesn't even need a regex. It would be much simpler if just used the URL API, which would allow you to write:["http", "https", "file", "ftp"].contains(url.scheme)