SwiftyJson - 从数组中删除空元素

问题描述 投票:1回答:1

我有一个来自JSON响应的数组。在这个数组中从0-20个位置。在每个位置都可以是图像或视频。从循环中我得到了带有图像和视频的网址。有时我有8张图片和2个视频,但有回复 - 8个网址的图片,2个网址的视频,6个空的网址。我如何删除空的视频网址?

edge.........   
  edges 
   0    
   node 
   __typename   "........"
   display_resources    
   0    {…}
   1    {…}
   2    {…}
   is_video     false
   edge.........{…}

   1    
   node 
 __typename "........"
   display_resources    
   0    {…}
   1    {…}
   2    {…}
   video_url      "https://...................fods1-1.fna.fbcdn.net/vp/64633261764d268afd5d6a654d845590/5A94EC04/t50.2886-16/27988152_1901827653461655_8604903370520797664_n.mp4"
   is_video  true
   edge.............    {…}

我的守则

var json = JSON(response.result.value ?? [])
...
if self.json["..........."].stringValue == ......... {
    if let slideShow = self.json["........"].array {
        for allMedia in slideShow {
            self............append(allMedia["node"][2]["src"].stringValue)
        }
        print("One of All Media \(self.graphSidecarImage[0])")
    }

    if let allVideo = self.json["........"][].array {
        for videos in allVideo {
            self.graphSidecarVideo.append(videos["node"]["video_url"].stringValue)
        }
        print("Video = \(self........)")
    }

} else ....

我的回复

One of All Media = 
https://...........fna.fbcdn.net/vp/873e7dd29985cbb359a6bed475b5eb4a/5B4BE8A4/t51.2885-15/e35/27877630_529424757457590_5205275138961965056_n.jpg


Video = ["",
“https://............fods1-1.fna.fbcdn.net./vp/4500643b92f7564ce0b1d03266bad3b8/5A963D84/t50.2886-16/27988152_1901827653461655_8604903370520797664_n.mp4”,
"",
“https://..............fods1-1.fna.fbcdn.net/vp/751ff5e4e64b0bb8e9f5766835bf64b8/5A96707B/t50.2886-16/28257257_152261638823479_2625135617605674147_n.mp4”,
"",
"",
"",
"",
"",
""]
arrays swift alamofire swifty-json
1个回答
3
投票

试试这个:

    private func prepare_array()
    {
        var array = ["", "not empty 1", "", "", "", "not empty 2", "not empty 3"]
        array = array.filter({ $0 != ""})
        print(array) 
        //["not empty 1", "not empty 2", "not empty 3"]
    }
© www.soinside.com 2019 - 2024. All rights reserved.