Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何将字符串数组转换为URL数组?_Swift - Fatal编程技术网

Swift 如何将字符串数组转换为URL数组?

Swift 如何将字符串数组转换为URL数组?,swift,Swift,需要将字符串值数组转换为URL数组。有人帮我在swift中将字符串数组转换为URL数组 let urls = ["www.1.com", "www.1.com", "www.1.com", "randomSmth"].compactMap { URL(string: $0) } “compactMap”确保它只忽略无效的URL 使用压缩映射过滤无效URL let arrayOfStrings = ["https://www.google.com","https://www.facebook.

需要将字符串值数组转换为URL数组。有人帮我在swift中将字符串数组转换为URL数组

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

“compactMap”确保它只忽略无效的URL

使用压缩映射过滤无效URL


let arrayOfStrings = ["https://www.google.com","https://www.facebook.com", "notRerallyAnURL"]
let arrayOfURLs = arrayOfStrings.compactMap { URL(string:$0) }

投票表决您的答案,因为您是第一个提供不包括强制展开的有效解决方案的人(刚刚出现了大量副本)。这应标记为正确答案。