0

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'

2
  • 1
    To check that a string is a valid URL, have a look here stackoverflow.com/questions/28079123/… Commented Sep 10, 2019 at 11:44
  • You shouldn't do URL operations like this on a raw 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) Commented Sep 10, 2019 at 12:58

1 Answer 1

2

[String: Any] is a dictionary , You need [[String: Any]]

func cleanArray(data:[[String: Any]]) -> Void { 
    let uris = data.filter{ ($0["url"] as! String).range(of: #"^(https?|file|ftp)"#, options: .regularExpression) != nil };
}

This could be more object oriented if you make it an array of a model

Sign up to request clarification or add additional context in comments.

Comments

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.