0

From server response i'm getting this 5 array of arrays (Some of them is array of arrays)

What i want is replace matching value from array "Legs" () with full array that have matching value. For example if value Legs["Id"] matching with value Itineraries ["OutboundLegId"] i want to replace full array "Itineraries" with this value

1st:

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;
            }
        );
    },

2nd:

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
        );
    },

that code shows how convert data in to the objects:

      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})

That what i want but not fully understand how to prevent to swift code:

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

}

3
  • Can you show code for your attempt at doing this? I don't fully understand the question so it will help to understand the outcome you're looking for. Commented Jan 21, 2017 at 22:06
  • @Frankie updated my answer. Commented Jan 21, 2017 at 22:52
  • Before you try to do this, first, convert this data to structs. Trying to work on JSON dictionaries is going to make this incredibly and unnecessarily complicated. You want a struct Leg and a struct Itinerary (and possibly also Flight, Agent, Carrier, etc). Taking the trouble to break this down to types will dramatically simplify your algorithms. Then you can easily write methods like hasMatchingItinerary or make Itinerary Equatable, or anything else you need. Commented Jan 21, 2017 at 23:03

1 Answer 1

1

Here's an example of how you might begin to convert your data to structs

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
    ...
}

Possible usage:

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

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

let containsLeg = someItinerary.contains(someLeg)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.