Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Array of string to Array of URL?

Tags:

swift

Need to convert array of string value to array of URL. Some one help me to convert the String array to URL array in swift.

like image 337
Siva N Avatar asked Dec 10 '22 03:12

Siva N


1 Answers

let urls = ["www.1.com", "www.1.com", "www.1.com", "randomSmth"].compactMap { URL(string: $0) }

"compactMap" ensures that it just ignores invalid urls.

like image 132
Michael Avatar answered Jan 14 '23 16:01

Michael