简体   繁体   English

将SwiftyJSON中的元素添加到Swift中的URL数组

[英]Add elements from SwiftyJSON to URL array in Swift

I'm retrieving a list of URL images using Alamofire. 我正在使用Alamofire检索URL图像列表。 The response is in JSON And I have used SwiftyJSON to parse and print each element. 响应是在JSON中,并且我使用了SwiftyJSON来解析和打印每个元素。 I want the URLs to be added into a URL array. 我希望将URL添加到URL数组中。 Below is the code I have used 以下是我使用的代码

var newArray = [URL(string: "http://www.tummyvision.com/users/uploads/gijovarghese141@gmail.com/photos/4.jpg")]

        Alamofire.request("http://www.tummyvision.com/users/login/get-images.php", parameters: parameters).responseData { response in
            let json = JSON(data: response.result.value!)
            for i in 0..<json.count
            {
                print(json[i]) // prints the correct url
                self.urlArray.append(json[i])
            }
        }

But it is giving me the following error: 但这给了我以下错误:

在此处输入图片说明

Try to use arrayValue property of JSON to access array also you need to convert String Url to URL object before adding it into Array of URL . 尝试使用arrayValue JSON的属性来访问数组也需要转换String的URL URL加入到它的前阵对象URL

if let urls = json.arrayValue {
    for url in urls {
        self.urlArray.append(URL(string:url))
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM