ios - 将数组追加到数组的数组中,如果它在内部具有相同的值。 swift

标签 ios arrays swift algorithm dictionary

从服务器响应我得到这 5 个数组数组(其中一些是数组数组)

我想要的是用具有匹配值的完整数组替换数组“Legs”() 中的匹配值。例如,如果值 Legs["Id"] 与值 Itineraries ["OutboundLegId"] 匹配,我想用这个值替换完整数组“Itineraries”

第一:

Itineraries =     (
            {
        BookingDetailsLink =             {
            Body = "";
            Method = PUT;
            Uri = "";
        };
        InboundLegId = "10081-1701231145--31722-1-16216-1701231854";
        OutboundLegId = "16216-1701222315--31722-1-10081-1701230921";
        PricingOptions =             (
                            {
                Agents =                     (
                    4132306
                );
                DeeplinkUrl = "http://partners.api";
                Price = 706;
                QuoteAgeInMinutes = 121;
            }
        );
    },

第二个:

Legs =     (
            {
        Arrival = "2017-01-22T20:33:00";
        Carriers =             (
            870
        );
        Departure = "2017-01-22T08:59:00";
        DestinationStation = 10081;
        Directionality = Outbound;
        Duration = 514;
        FlightNumbers =             (
                            {
                CarrierId = 870;
                FlightNumber = 2288;
            },
                            {
                CarrierId = 870;
                FlightNumber = 178;
            }
        );
        Id = "16216-1701220859--32171-1-10081-1701222033";
        JourneyMode = Flight;
        OperatingCarriers =             (
            870
        );
        OriginStation = 16216;
        SegmentIds =             (
            0,
            1
        );
        Stops =             (
            13411
        );
    },

该代码显示了如何将数据转换为对象:

      let agents: Array = json["Agents"].arrayValue
      let carriers: Array = json["Carriers"].arrayValue
      let places: Array = json["Places"].arrayValue
      let legs: Array = json["Legs"].arrayValue
      let ss: Array = json["Itineraries"].arrayValue
      let itineraries: Array = json["Itineraries"].arrayValue.flatMap({$0["PricingOptions"].arrayValue})

那是我想要但不完全理解如何防止 swift 代码:

for legID in legs {
if legs.id.match with itineraries.id {
        legs.id.apped(itineraries)
}

最佳答案

这是一个示例,说明您可以如何开始将数据转换为结构

struct Itinerary {

    struct PricingOptions {
        var deepLinkURL: String?
        ...

        init(dictionary: [String : Any]?) {
            self.deepLinkURL = dictionary?["DeepLinkURL"] as? String
            ...
        }
    }

    var inboundLegID: String?
    var outboundLegID: String?
    var pricingOptions: [PricingOptions]?
    ...

    init(dictionary: [String : Any]) {

        self.inboundLegID = dictionary["InboundLegID"] as? String
        self.outboundLegID = dictionary["OutboundLegID"] as? String

        if let options = dictionary["PricingOptions"] as? [[String : Any]] {
            self.pricingOptions = options.map({ PricingOptions(dictionary: $0) })
        }

        ...
    }

    func contains(leg: Leg) -> Bool {
        return leg.id == inboundLegID || leg.id == outboundLegID
    }
}

struct Leg {
//  apply same idea here
    ...
}

可能的用法:

let allItineraries = itinerariesJSON.map({ Itinerary($0) })

let someItinerary = allItineraries.find({ $0.contains(someLeg) })

let containsLeg = someItinerary.contains(someLeg)

关于ios - 将数组追加到数组的数组中,如果它在内部具有相同的值。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41784601/

相关文章:

ios - CFDictionaryGetValue 无法使用 CFStringRef 调用

ios - Facebook Connect API 未在 iPhone 中创建 session

ios - AVAudioPlayer 在文件结束之前停止

python - NetworkX:从 2D numpy 数组创建的常规图会产生不匹配

java - for循环和数组比较问题

ios - 带有与 ViewController 分离的 Controller 的 UITableView

ios - AVPlayer 不断添加流而不是替换

ios - SDWebImage仅是ARC。为项目打开ARC或使用-fobjc-arc标志IOS

ios - UITableview contentoffset 有时不起作用

javascript - 无法从日期对象javascript获取日期和月份