How to put the of first and last element of Array in Swift

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I have a dictionary like this



["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]


Now I am storing all the dictionary value in array like this



var priceRange: [String] = [String]()
if let obj = currentFilters["Price"] as? [String]
self.priceRange = obj
printD(self.priceRange)



And by the use of Array.first and Array.last method I will get the values of first element and last element of my array.



let first = priceRange.first ?? "" // will get("[$00.00 - $200.00]")
let last = priceRange.last ?? "" // will get("[$600.00 - $800.00]")


But What I actually want is I want the $00.00 from first and $800 from last to make the desired combination of [$00.00 - $800.00].



How can I do this. Please help?










share|improve this question



























    up vote
    6
    down vote

    favorite












    I have a dictionary like this



    ["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]


    Now I am storing all the dictionary value in array like this



    var priceRange: [String] = [String]()
    if let obj = currentFilters["Price"] as? [String]
    self.priceRange = obj
    printD(self.priceRange)



    And by the use of Array.first and Array.last method I will get the values of first element and last element of my array.



    let first = priceRange.first ?? "" // will get("[$00.00 - $200.00]")
    let last = priceRange.last ?? "" // will get("[$600.00 - $800.00]")


    But What I actually want is I want the $00.00 from first and $800 from last to make the desired combination of [$00.00 - $800.00].



    How can I do this. Please help?










    share|improve this question

























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I have a dictionary like this



      ["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]


      Now I am storing all the dictionary value in array like this



      var priceRange: [String] = [String]()
      if let obj = currentFilters["Price"] as? [String]
      self.priceRange = obj
      printD(self.priceRange)



      And by the use of Array.first and Array.last method I will get the values of first element and last element of my array.



      let first = priceRange.first ?? "" // will get("[$00.00 - $200.00]")
      let last = priceRange.last ?? "" // will get("[$600.00 - $800.00]")


      But What I actually want is I want the $00.00 from first and $800 from last to make the desired combination of [$00.00 - $800.00].



      How can I do this. Please help?










      share|improve this question















      I have a dictionary like this



      ["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]


      Now I am storing all the dictionary value in array like this



      var priceRange: [String] = [String]()
      if let obj = currentFilters["Price"] as? [String]
      self.priceRange = obj
      printD(self.priceRange)



      And by the use of Array.first and Array.last method I will get the values of first element and last element of my array.



      let first = priceRange.first ?? "" // will get("[$00.00 - $200.00]")
      let last = priceRange.last ?? "" // will get("[$600.00 - $800.00]")


      But What I actually want is I want the $00.00 from first and $800 from last to make the desired combination of [$00.00 - $800.00].



      How can I do this. Please help?







      ios arrays swift






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 57 mins ago

























      asked 1 hour ago









      wings

      722319




      722319






















          8 Answers
          8






          active

          oldest

          votes

















          up vote
          2
          down vote













          You can do the following:



           let r = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
          .reduce("", +) //Combine all strings
          .components(separatedBy: " - ") //Split the result
          .map( String($0) ) //Convert from SubString to String
          print(r.first) //prints $00.00
          print(r.last) // prints $800.00

          let newRange = [r.first!, r.last!].joined(separator: " - ")





          share|improve this answer






















          • but sir by use of these I will get 00.00 and 800.00 but i actually want [$00.00 - $800.00]
            – wings
            53 mins ago










          • @wings just format the new string with first and last the way you want it.
            – Rakesha Shastri
            50 mins ago











          • Updated to your requirement @wings
            – CZ54
            48 mins ago










          • @CZ54 Sir it is okay to use ! in that code because sometimes I won't get the values
            – wings
            42 mins ago










          • I'm assuming you array is not empty, otherwise your range is ""
            – CZ54
            39 mins ago

















          up vote
          2
          down vote













          func priceRange(with priceRanges: [String]) -> String? 
          guard
          let min = priceRanges.first?.components(separatedBy: " - ").first,
          let max = priceRanges.last?.components(separatedBy: " - ").last else return nil

          return "[(min) - (max)]"


          print(priceRange(
          with: [
          "$00.00 - $200.00",
          "$200.00 - $400.00",
          "$600.00 - $800.00"
          ]
          ))





          share|improve this answer



























            up vote
            1
            down vote













            You can use split to split up the range based on the - character than remove the whitespaces.



            let first = priceRange.first?.split(separator: "-").first?.trimmingCharacters(in: .whitespaces) ?? ""
            let last = priceRange.last?.split(separator: "-").last?.trimmingCharacters(in: .whitespaces) ?? ""





            share|improve this answer



























              up vote
              1
              down vote













              Use this You can find your values:



              if let obj = priceRange as? [String] 
              let max = priceRange.max()
              let min = priceRange.min()
              print(max?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).max()) //800
              print(min?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).min()) //00






              share|improve this answer



























                up vote
                1
                down vote













                You need to take first value ("$00.00 - $200.00"), then last value ("$600.00 - $800.00"), then split them by "-" symbol and take first and last values respectively and combine it to single string.



                let currentFilters = ["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]

                var priceRange: [String] = [String]()
                if let obj = currentFilters["Price"] as? [String]
                priceRange = obj
                print(priceRange)


                let first = priceRange.first!.split(separator: "-").first!
                let last = priceRange.last!.split(separator: "-").last!

                let range = "(first) - (last)"


                For better optionals handling you can use this (NB, I'm following my over-descriptive coding style. This code can be much more compact)



                func totalRange(filters: [String]?) -> String? 
                guard let filters = filters else return nil
                guard filters.isEmpty == false else return nil
                guard let startComponents = priceRange.first?.split(separator: "-"), startComponents.count == 2 else
                fatalError("Unexpected Filter format for first filter") // or `return nil`

                guard let endComponents = priceRange.last?.split(separator: "-"), endComponents.count == 2 else
                fatalError("Unexpected Filter format for last filter") // or `return nil`

                return "(startComponents.first!) - (endComponents.last!)"

                let range = totalRange(filters: currentFilters["Price"])

                let range1 = totalRange(filters: currentFilters["Not Exists"])


                Past the code above to the playground. It can be written much shorter way, but I kept it like that for the sake of descriptivity






                share|improve this answer






















                • Sir what if my dictonary is empty
                  – wings
                  35 mins ago










                • If your dictionary is empty, my code will crash your application :). It was made to demonstrate the way how you can achieve desired behaviour. Optional values handling is a subject of further implementation.
                  – fewlinesofcode
                  31 mins ago










                • How can I handle this
                  – wings
                  19 mins ago










                • @wings, updated my answer
                  – fewlinesofcode
                  5 mins ago

















                up vote
                0
                down vote













                //use components(separatedBy:) :



                let prices = f.components(separatedBy: " - ")



                prices.first // $600.00



                prices.last // $800.00






                share|improve this answer








                New contributor




                Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.













                • 1




                  Learn how to use code block
                  – è•­ç‚ºå…ƒ
                  48 mins ago

















                up vote
                0
                down vote













                You could also use String.components(separatedBy:):



                var text = "100$ - 200$"
                var new = text.components(separatedBy: " - ")[0]
                print(new) // Prints 100$





                share|improve this answer



























                  up vote
                  0
                  down vote













                  let amountsArray = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                  let amounts = amountsArray.reduce("") $0 + $1 .split(separator: "-")
                  if let first = amounts.first, let last = amounts.last
                  print("(first)-(last)")






                  share|improve this answer






















                  • You get an extra " " when you split with "-".
                    – Rakesha Shastri
                    8 mins ago










                  • You are right @RakeshaShastri. Thanks..!! I've updated the answer
                    – Sateesh
                    4 mins ago











                  • It is still wrong. Did you check it?
                    – Rakesha Shastri
                    3 mins ago










                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53151493%2fhow-to-put-the-of-first-and-last-element-of-array-in-swift%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  8 Answers
                  8






                  active

                  oldest

                  votes








                  8 Answers
                  8






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  2
                  down vote













                  You can do the following:



                   let r = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                  .reduce("", +) //Combine all strings
                  .components(separatedBy: " - ") //Split the result
                  .map( String($0) ) //Convert from SubString to String
                  print(r.first) //prints $00.00
                  print(r.last) // prints $800.00

                  let newRange = [r.first!, r.last!].joined(separator: " - ")





                  share|improve this answer






















                  • but sir by use of these I will get 00.00 and 800.00 but i actually want [$00.00 - $800.00]
                    – wings
                    53 mins ago










                  • @wings just format the new string with first and last the way you want it.
                    – Rakesha Shastri
                    50 mins ago











                  • Updated to your requirement @wings
                    – CZ54
                    48 mins ago










                  • @CZ54 Sir it is okay to use ! in that code because sometimes I won't get the values
                    – wings
                    42 mins ago










                  • I'm assuming you array is not empty, otherwise your range is ""
                    – CZ54
                    39 mins ago














                  up vote
                  2
                  down vote













                  You can do the following:



                   let r = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                  .reduce("", +) //Combine all strings
                  .components(separatedBy: " - ") //Split the result
                  .map( String($0) ) //Convert from SubString to String
                  print(r.first) //prints $00.00
                  print(r.last) // prints $800.00

                  let newRange = [r.first!, r.last!].joined(separator: " - ")





                  share|improve this answer






















                  • but sir by use of these I will get 00.00 and 800.00 but i actually want [$00.00 - $800.00]
                    – wings
                    53 mins ago










                  • @wings just format the new string with first and last the way you want it.
                    – Rakesha Shastri
                    50 mins ago











                  • Updated to your requirement @wings
                    – CZ54
                    48 mins ago










                  • @CZ54 Sir it is okay to use ! in that code because sometimes I won't get the values
                    – wings
                    42 mins ago










                  • I'm assuming you array is not empty, otherwise your range is ""
                    – CZ54
                    39 mins ago












                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  You can do the following:



                   let r = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                  .reduce("", +) //Combine all strings
                  .components(separatedBy: " - ") //Split the result
                  .map( String($0) ) //Convert from SubString to String
                  print(r.first) //prints $00.00
                  print(r.last) // prints $800.00

                  let newRange = [r.first!, r.last!].joined(separator: " - ")





                  share|improve this answer














                  You can do the following:



                   let r = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                  .reduce("", +) //Combine all strings
                  .components(separatedBy: " - ") //Split the result
                  .map( String($0) ) //Convert from SubString to String
                  print(r.first) //prints $00.00
                  print(r.last) // prints $800.00

                  let newRange = [r.first!, r.last!].joined(separator: " - ")






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 49 mins ago

























                  answered 55 mins ago









                  CZ54

                  3,16411432




                  3,16411432











                  • but sir by use of these I will get 00.00 and 800.00 but i actually want [$00.00 - $800.00]
                    – wings
                    53 mins ago










                  • @wings just format the new string with first and last the way you want it.
                    – Rakesha Shastri
                    50 mins ago











                  • Updated to your requirement @wings
                    – CZ54
                    48 mins ago










                  • @CZ54 Sir it is okay to use ! in that code because sometimes I won't get the values
                    – wings
                    42 mins ago










                  • I'm assuming you array is not empty, otherwise your range is ""
                    – CZ54
                    39 mins ago
















                  • but sir by use of these I will get 00.00 and 800.00 but i actually want [$00.00 - $800.00]
                    – wings
                    53 mins ago










                  • @wings just format the new string with first and last the way you want it.
                    – Rakesha Shastri
                    50 mins ago











                  • Updated to your requirement @wings
                    – CZ54
                    48 mins ago










                  • @CZ54 Sir it is okay to use ! in that code because sometimes I won't get the values
                    – wings
                    42 mins ago










                  • I'm assuming you array is not empty, otherwise your range is ""
                    – CZ54
                    39 mins ago















                  but sir by use of these I will get 00.00 and 800.00 but i actually want [$00.00 - $800.00]
                  – wings
                  53 mins ago




                  but sir by use of these I will get 00.00 and 800.00 but i actually want [$00.00 - $800.00]
                  – wings
                  53 mins ago












                  @wings just format the new string with first and last the way you want it.
                  – Rakesha Shastri
                  50 mins ago





                  @wings just format the new string with first and last the way you want it.
                  – Rakesha Shastri
                  50 mins ago













                  Updated to your requirement @wings
                  – CZ54
                  48 mins ago




                  Updated to your requirement @wings
                  – CZ54
                  48 mins ago












                  @CZ54 Sir it is okay to use ! in that code because sometimes I won't get the values
                  – wings
                  42 mins ago




                  @CZ54 Sir it is okay to use ! in that code because sometimes I won't get the values
                  – wings
                  42 mins ago












                  I'm assuming you array is not empty, otherwise your range is ""
                  – CZ54
                  39 mins ago




                  I'm assuming you array is not empty, otherwise your range is ""
                  – CZ54
                  39 mins ago












                  up vote
                  2
                  down vote













                  func priceRange(with priceRanges: [String]) -> String? 
                  guard
                  let min = priceRanges.first?.components(separatedBy: " - ").first,
                  let max = priceRanges.last?.components(separatedBy: " - ").last else return nil

                  return "[(min) - (max)]"


                  print(priceRange(
                  with: [
                  "$00.00 - $200.00",
                  "$200.00 - $400.00",
                  "$600.00 - $800.00"
                  ]
                  ))





                  share|improve this answer
























                    up vote
                    2
                    down vote













                    func priceRange(with priceRanges: [String]) -> String? 
                    guard
                    let min = priceRanges.first?.components(separatedBy: " - ").first,
                    let max = priceRanges.last?.components(separatedBy: " - ").last else return nil

                    return "[(min) - (max)]"


                    print(priceRange(
                    with: [
                    "$00.00 - $200.00",
                    "$200.00 - $400.00",
                    "$600.00 - $800.00"
                    ]
                    ))





                    share|improve this answer






















                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      func priceRange(with priceRanges: [String]) -> String? 
                      guard
                      let min = priceRanges.first?.components(separatedBy: " - ").first,
                      let max = priceRanges.last?.components(separatedBy: " - ").last else return nil

                      return "[(min) - (max)]"


                      print(priceRange(
                      with: [
                      "$00.00 - $200.00",
                      "$200.00 - $400.00",
                      "$600.00 - $800.00"
                      ]
                      ))





                      share|improve this answer












                      func priceRange(with priceRanges: [String]) -> String? 
                      guard
                      let min = priceRanges.first?.components(separatedBy: " - ").first,
                      let max = priceRanges.last?.components(separatedBy: " - ").last else return nil

                      return "[(min) - (max)]"


                      print(priceRange(
                      with: [
                      "$00.00 - $200.00",
                      "$200.00 - $400.00",
                      "$600.00 - $800.00"
                      ]
                      ))






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 48 mins ago









                      Callam

                      7,26821321




                      7,26821321




















                          up vote
                          1
                          down vote













                          You can use split to split up the range based on the - character than remove the whitespaces.



                          let first = priceRange.first?.split(separator: "-").first?.trimmingCharacters(in: .whitespaces) ?? ""
                          let last = priceRange.last?.split(separator: "-").last?.trimmingCharacters(in: .whitespaces) ?? ""





                          share|improve this answer
























                            up vote
                            1
                            down vote













                            You can use split to split up the range based on the - character than remove the whitespaces.



                            let first = priceRange.first?.split(separator: "-").first?.trimmingCharacters(in: .whitespaces) ?? ""
                            let last = priceRange.last?.split(separator: "-").last?.trimmingCharacters(in: .whitespaces) ?? ""





                            share|improve this answer






















                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              You can use split to split up the range based on the - character than remove the whitespaces.



                              let first = priceRange.first?.split(separator: "-").first?.trimmingCharacters(in: .whitespaces) ?? ""
                              let last = priceRange.last?.split(separator: "-").last?.trimmingCharacters(in: .whitespaces) ?? ""





                              share|improve this answer












                              You can use split to split up the range based on the - character than remove the whitespaces.



                              let first = priceRange.first?.split(separator: "-").first?.trimmingCharacters(in: .whitespaces) ?? ""
                              let last = priceRange.last?.split(separator: "-").last?.trimmingCharacters(in: .whitespaces) ?? ""






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 54 mins ago









                              Dávid Pásztor

                              18.4k62547




                              18.4k62547




















                                  up vote
                                  1
                                  down vote













                                  Use this You can find your values:



                                  if let obj = priceRange as? [String] 
                                  let max = priceRange.max()
                                  let min = priceRange.min()
                                  print(max?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).max()) //800
                                  print(min?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).min()) //00






                                  share|improve this answer
























                                    up vote
                                    1
                                    down vote













                                    Use this You can find your values:



                                    if let obj = priceRange as? [String] 
                                    let max = priceRange.max()
                                    let min = priceRange.min()
                                    print(max?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).max()) //800
                                    print(min?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).min()) //00






                                    share|improve this answer






















                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      Use this You can find your values:



                                      if let obj = priceRange as? [String] 
                                      let max = priceRange.max()
                                      let min = priceRange.min()
                                      print(max?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).max()) //800
                                      print(min?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).min()) //00






                                      share|improve this answer












                                      Use this You can find your values:



                                      if let obj = priceRange as? [String] 
                                      let max = priceRange.max()
                                      let min = priceRange.min()
                                      print(max?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).max()) //800
                                      print(min?.components(separatedBy: " - ").map($0.trimmingCharacters(in: .whitespacesAndNewlines)).min()) //00







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 34 mins ago









                                      Pushp

                                      27029




                                      27029




















                                          up vote
                                          1
                                          down vote













                                          You need to take first value ("$00.00 - $200.00"), then last value ("$600.00 - $800.00"), then split them by "-" symbol and take first and last values respectively and combine it to single string.



                                          let currentFilters = ["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]

                                          var priceRange: [String] = [String]()
                                          if let obj = currentFilters["Price"] as? [String]
                                          priceRange = obj
                                          print(priceRange)


                                          let first = priceRange.first!.split(separator: "-").first!
                                          let last = priceRange.last!.split(separator: "-").last!

                                          let range = "(first) - (last)"


                                          For better optionals handling you can use this (NB, I'm following my over-descriptive coding style. This code can be much more compact)



                                          func totalRange(filters: [String]?) -> String? 
                                          guard let filters = filters else return nil
                                          guard filters.isEmpty == false else return nil
                                          guard let startComponents = priceRange.first?.split(separator: "-"), startComponents.count == 2 else
                                          fatalError("Unexpected Filter format for first filter") // or `return nil`

                                          guard let endComponents = priceRange.last?.split(separator: "-"), endComponents.count == 2 else
                                          fatalError("Unexpected Filter format for last filter") // or `return nil`

                                          return "(startComponents.first!) - (endComponents.last!)"

                                          let range = totalRange(filters: currentFilters["Price"])

                                          let range1 = totalRange(filters: currentFilters["Not Exists"])


                                          Past the code above to the playground. It can be written much shorter way, but I kept it like that for the sake of descriptivity






                                          share|improve this answer






















                                          • Sir what if my dictonary is empty
                                            – wings
                                            35 mins ago










                                          • If your dictionary is empty, my code will crash your application :). It was made to demonstrate the way how you can achieve desired behaviour. Optional values handling is a subject of further implementation.
                                            – fewlinesofcode
                                            31 mins ago










                                          • How can I handle this
                                            – wings
                                            19 mins ago










                                          • @wings, updated my answer
                                            – fewlinesofcode
                                            5 mins ago














                                          up vote
                                          1
                                          down vote













                                          You need to take first value ("$00.00 - $200.00"), then last value ("$600.00 - $800.00"), then split them by "-" symbol and take first and last values respectively and combine it to single string.



                                          let currentFilters = ["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]

                                          var priceRange: [String] = [String]()
                                          if let obj = currentFilters["Price"] as? [String]
                                          priceRange = obj
                                          print(priceRange)


                                          let first = priceRange.first!.split(separator: "-").first!
                                          let last = priceRange.last!.split(separator: "-").last!

                                          let range = "(first) - (last)"


                                          For better optionals handling you can use this (NB, I'm following my over-descriptive coding style. This code can be much more compact)



                                          func totalRange(filters: [String]?) -> String? 
                                          guard let filters = filters else return nil
                                          guard filters.isEmpty == false else return nil
                                          guard let startComponents = priceRange.first?.split(separator: "-"), startComponents.count == 2 else
                                          fatalError("Unexpected Filter format for first filter") // or `return nil`

                                          guard let endComponents = priceRange.last?.split(separator: "-"), endComponents.count == 2 else
                                          fatalError("Unexpected Filter format for last filter") // or `return nil`

                                          return "(startComponents.first!) - (endComponents.last!)"

                                          let range = totalRange(filters: currentFilters["Price"])

                                          let range1 = totalRange(filters: currentFilters["Not Exists"])


                                          Past the code above to the playground. It can be written much shorter way, but I kept it like that for the sake of descriptivity






                                          share|improve this answer






















                                          • Sir what if my dictonary is empty
                                            – wings
                                            35 mins ago










                                          • If your dictionary is empty, my code will crash your application :). It was made to demonstrate the way how you can achieve desired behaviour. Optional values handling is a subject of further implementation.
                                            – fewlinesofcode
                                            31 mins ago










                                          • How can I handle this
                                            – wings
                                            19 mins ago










                                          • @wings, updated my answer
                                            – fewlinesofcode
                                            5 mins ago












                                          up vote
                                          1
                                          down vote










                                          up vote
                                          1
                                          down vote









                                          You need to take first value ("$00.00 - $200.00"), then last value ("$600.00 - $800.00"), then split them by "-" symbol and take first and last values respectively and combine it to single string.



                                          let currentFilters = ["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]

                                          var priceRange: [String] = [String]()
                                          if let obj = currentFilters["Price"] as? [String]
                                          priceRange = obj
                                          print(priceRange)


                                          let first = priceRange.first!.split(separator: "-").first!
                                          let last = priceRange.last!.split(separator: "-").last!

                                          let range = "(first) - (last)"


                                          For better optionals handling you can use this (NB, I'm following my over-descriptive coding style. This code can be much more compact)



                                          func totalRange(filters: [String]?) -> String? 
                                          guard let filters = filters else return nil
                                          guard filters.isEmpty == false else return nil
                                          guard let startComponents = priceRange.first?.split(separator: "-"), startComponents.count == 2 else
                                          fatalError("Unexpected Filter format for first filter") // or `return nil`

                                          guard let endComponents = priceRange.last?.split(separator: "-"), endComponents.count == 2 else
                                          fatalError("Unexpected Filter format for last filter") // or `return nil`

                                          return "(startComponents.first!) - (endComponents.last!)"

                                          let range = totalRange(filters: currentFilters["Price"])

                                          let range1 = totalRange(filters: currentFilters["Not Exists"])


                                          Past the code above to the playground. It can be written much shorter way, but I kept it like that for the sake of descriptivity






                                          share|improve this answer














                                          You need to take first value ("$00.00 - $200.00"), then last value ("$600.00 - $800.00"), then split them by "-" symbol and take first and last values respectively and combine it to single string.



                                          let currentFilters = ["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]

                                          var priceRange: [String] = [String]()
                                          if let obj = currentFilters["Price"] as? [String]
                                          priceRange = obj
                                          print(priceRange)


                                          let first = priceRange.first!.split(separator: "-").first!
                                          let last = priceRange.last!.split(separator: "-").last!

                                          let range = "(first) - (last)"


                                          For better optionals handling you can use this (NB, I'm following my over-descriptive coding style. This code can be much more compact)



                                          func totalRange(filters: [String]?) -> String? 
                                          guard let filters = filters else return nil
                                          guard filters.isEmpty == false else return nil
                                          guard let startComponents = priceRange.first?.split(separator: "-"), startComponents.count == 2 else
                                          fatalError("Unexpected Filter format for first filter") // or `return nil`

                                          guard let endComponents = priceRange.last?.split(separator: "-"), endComponents.count == 2 else
                                          fatalError("Unexpected Filter format for last filter") // or `return nil`

                                          return "(startComponents.first!) - (endComponents.last!)"

                                          let range = totalRange(filters: currentFilters["Price"])

                                          let range1 = totalRange(filters: currentFilters["Not Exists"])


                                          Past the code above to the playground. It can be written much shorter way, but I kept it like that for the sake of descriptivity







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited 6 mins ago

























                                          answered 52 mins ago









                                          fewlinesofcode

                                          1,129310




                                          1,129310











                                          • Sir what if my dictonary is empty
                                            – wings
                                            35 mins ago










                                          • If your dictionary is empty, my code will crash your application :). It was made to demonstrate the way how you can achieve desired behaviour. Optional values handling is a subject of further implementation.
                                            – fewlinesofcode
                                            31 mins ago










                                          • How can I handle this
                                            – wings
                                            19 mins ago










                                          • @wings, updated my answer
                                            – fewlinesofcode
                                            5 mins ago
















                                          • Sir what if my dictonary is empty
                                            – wings
                                            35 mins ago










                                          • If your dictionary is empty, my code will crash your application :). It was made to demonstrate the way how you can achieve desired behaviour. Optional values handling is a subject of further implementation.
                                            – fewlinesofcode
                                            31 mins ago










                                          • How can I handle this
                                            – wings
                                            19 mins ago










                                          • @wings, updated my answer
                                            – fewlinesofcode
                                            5 mins ago















                                          Sir what if my dictonary is empty
                                          – wings
                                          35 mins ago




                                          Sir what if my dictonary is empty
                                          – wings
                                          35 mins ago












                                          If your dictionary is empty, my code will crash your application :). It was made to demonstrate the way how you can achieve desired behaviour. Optional values handling is a subject of further implementation.
                                          – fewlinesofcode
                                          31 mins ago




                                          If your dictionary is empty, my code will crash your application :). It was made to demonstrate the way how you can achieve desired behaviour. Optional values handling is a subject of further implementation.
                                          – fewlinesofcode
                                          31 mins ago












                                          How can I handle this
                                          – wings
                                          19 mins ago




                                          How can I handle this
                                          – wings
                                          19 mins ago












                                          @wings, updated my answer
                                          – fewlinesofcode
                                          5 mins ago




                                          @wings, updated my answer
                                          – fewlinesofcode
                                          5 mins ago










                                          up vote
                                          0
                                          down vote













                                          //use components(separatedBy:) :



                                          let prices = f.components(separatedBy: " - ")



                                          prices.first // $600.00



                                          prices.last // $800.00






                                          share|improve this answer








                                          New contributor




                                          Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.













                                          • 1




                                            Learn how to use code block
                                            – è•­ç‚ºå…ƒ
                                            48 mins ago














                                          up vote
                                          0
                                          down vote













                                          //use components(separatedBy:) :



                                          let prices = f.components(separatedBy: " - ")



                                          prices.first // $600.00



                                          prices.last // $800.00






                                          share|improve this answer








                                          New contributor




                                          Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.













                                          • 1




                                            Learn how to use code block
                                            – è•­ç‚ºå…ƒ
                                            48 mins ago












                                          up vote
                                          0
                                          down vote










                                          up vote
                                          0
                                          down vote









                                          //use components(separatedBy:) :



                                          let prices = f.components(separatedBy: " - ")



                                          prices.first // $600.00



                                          prices.last // $800.00






                                          share|improve this answer








                                          New contributor




                                          Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.









                                          //use components(separatedBy:) :



                                          let prices = f.components(separatedBy: " - ")



                                          prices.first // $600.00



                                          prices.last // $800.00







                                          share|improve this answer








                                          New contributor




                                          Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.









                                          share|improve this answer



                                          share|improve this answer






                                          New contributor




                                          Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.









                                          answered 52 mins ago









                                          Djamal

                                          1




                                          1




                                          New contributor




                                          Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.





                                          New contributor





                                          Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.






                                          Djamal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                          Check out our Code of Conduct.







                                          • 1




                                            Learn how to use code block
                                            – è•­ç‚ºå…ƒ
                                            48 mins ago












                                          • 1




                                            Learn how to use code block
                                            – è•­ç‚ºå…ƒ
                                            48 mins ago







                                          1




                                          1




                                          Learn how to use code block
                                          – è•­ç‚ºå…ƒ
                                          48 mins ago




                                          Learn how to use code block
                                          – è•­ç‚ºå…ƒ
                                          48 mins ago










                                          up vote
                                          0
                                          down vote













                                          You could also use String.components(separatedBy:):



                                          var text = "100$ - 200$"
                                          var new = text.components(separatedBy: " - ")[0]
                                          print(new) // Prints 100$





                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            You could also use String.components(separatedBy:):



                                            var text = "100$ - 200$"
                                            var new = text.components(separatedBy: " - ")[0]
                                            print(new) // Prints 100$





                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              You could also use String.components(separatedBy:):



                                              var text = "100$ - 200$"
                                              var new = text.components(separatedBy: " - ")[0]
                                              print(new) // Prints 100$





                                              share|improve this answer












                                              You could also use String.components(separatedBy:):



                                              var text = "100$ - 200$"
                                              var new = text.components(separatedBy: " - ")[0]
                                              print(new) // Prints 100$






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered 52 mins ago









                                              user2531284

                                              8519




                                              8519




















                                                  up vote
                                                  0
                                                  down vote













                                                  let amountsArray = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                                                  let amounts = amountsArray.reduce("") $0 + $1 .split(separator: "-")
                                                  if let first = amounts.first, let last = amounts.last
                                                  print("(first)-(last)")






                                                  share|improve this answer






















                                                  • You get an extra " " when you split with "-".
                                                    – Rakesha Shastri
                                                    8 mins ago










                                                  • You are right @RakeshaShastri. Thanks..!! I've updated the answer
                                                    – Sateesh
                                                    4 mins ago











                                                  • It is still wrong. Did you check it?
                                                    – Rakesha Shastri
                                                    3 mins ago














                                                  up vote
                                                  0
                                                  down vote













                                                  let amountsArray = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                                                  let amounts = amountsArray.reduce("") $0 + $1 .split(separator: "-")
                                                  if let first = amounts.first, let last = amounts.last
                                                  print("(first)-(last)")






                                                  share|improve this answer






















                                                  • You get an extra " " when you split with "-".
                                                    – Rakesha Shastri
                                                    8 mins ago










                                                  • You are right @RakeshaShastri. Thanks..!! I've updated the answer
                                                    – Sateesh
                                                    4 mins ago











                                                  • It is still wrong. Did you check it?
                                                    – Rakesha Shastri
                                                    3 mins ago












                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  let amountsArray = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                                                  let amounts = amountsArray.reduce("") $0 + $1 .split(separator: "-")
                                                  if let first = amounts.first, let last = amounts.last
                                                  print("(first)-(last)")






                                                  share|improve this answer














                                                  let amountsArray = ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]
                                                  let amounts = amountsArray.reduce("") $0 + $1 .split(separator: "-")
                                                  if let first = amounts.first, let last = amounts.last
                                                  print("(first)-(last)")







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited 4 mins ago

























                                                  answered 50 mins ago









                                                  Sateesh

                                                  759310




                                                  759310











                                                  • You get an extra " " when you split with "-".
                                                    – Rakesha Shastri
                                                    8 mins ago










                                                  • You are right @RakeshaShastri. Thanks..!! I've updated the answer
                                                    – Sateesh
                                                    4 mins ago











                                                  • It is still wrong. Did you check it?
                                                    – Rakesha Shastri
                                                    3 mins ago
















                                                  • You get an extra " " when you split with "-".
                                                    – Rakesha Shastri
                                                    8 mins ago










                                                  • You are right @RakeshaShastri. Thanks..!! I've updated the answer
                                                    – Sateesh
                                                    4 mins ago











                                                  • It is still wrong. Did you check it?
                                                    – Rakesha Shastri
                                                    3 mins ago















                                                  You get an extra " " when you split with "-".
                                                  – Rakesha Shastri
                                                  8 mins ago




                                                  You get an extra " " when you split with "-".
                                                  – Rakesha Shastri
                                                  8 mins ago












                                                  You are right @RakeshaShastri. Thanks..!! I've updated the answer
                                                  – Sateesh
                                                  4 mins ago





                                                  You are right @RakeshaShastri. Thanks..!! I've updated the answer
                                                  – Sateesh
                                                  4 mins ago













                                                  It is still wrong. Did you check it?
                                                  – Rakesha Shastri
                                                  3 mins ago




                                                  It is still wrong. Did you check it?
                                                  – Rakesha Shastri
                                                  3 mins ago

















                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53151493%2fhow-to-put-the-of-first-and-last-element-of-array-in-swift%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Comments

                                                  Popular posts from this blog

                                                  What does second last employer means? [closed]

                                                  List of Gilmore Girls characters

                                                  Confectionery