【问题标题】:SwiftyJson - Remove empty elements from arraySwiftyJson - 从数组中删除空元素
【发布时间】:2018-08-06 11:14:40
【问题描述】:

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

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】:

    试试这个:

        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"]
        }
    

    【讨论】:

    • filter{ !$0.isEmpty }
    • 您好,感谢您的回复。但我需要删除一个空结果。这没有用(((
    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多