How to put the of first and last element of Array in Swift
Clash 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?
ios arrays swift
add a comment |Â
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?
ios arrays swift
add a comment |Â
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?
ios arrays swift
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
ios arrays swift
edited 57 mins ago
asked 1 hour ago


wings
722319
722319
add a comment |Â
add a comment |Â
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: " - ")
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
add a comment |Â
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"
]
))
add a comment |Â
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) ?? ""
add a comment |Â
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
add a comment |Â
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
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
add a comment |Â
up vote
0
down vote
//use components(separatedBy:) :
let prices = f.components(separatedBy: " - ")
prices.first // $600.00
prices.last // $800.00
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
add a comment |Â
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$
add a comment |Â
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)")
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
add a comment |Â
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: " - ")
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
add a comment |Â
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: " - ")
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
add a comment |Â
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: " - ")
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: " - ")
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
add a comment |Â
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
add a comment |Â
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"
]
))
add a comment |Â
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"
]
))
add a comment |Â
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"
]
))
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"
]
))
answered 48 mins ago


Callam
7,26821321
7,26821321
add a comment |Â
add a comment |Â
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) ?? ""
add a comment |Â
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) ?? ""
add a comment |Â
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) ?? ""
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) ?? ""
answered 54 mins ago
Dávid Pásztor
18.4k62547
18.4k62547
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered 34 mins ago


Pushp
27029
27029
add a comment |Â
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
up vote
0
down vote
//use components(separatedBy:) :
let prices = f.components(separatedBy: " - ")
prices.first // $600.00
prices.last // $800.00
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
add a comment |Â
up vote
0
down vote
//use components(separatedBy:) :
let prices = f.components(separatedBy: " - ")
prices.first // $600.00
prices.last // $800.00
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
add a comment |Â
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
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
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.
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
add a comment |Â
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
add a comment |Â
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$
add a comment |Â
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$
add a comment |Â
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$
You could also use String.components(separatedBy:):
var text = "100$ - 200$"
var new = text.components(separatedBy: " - ")[0]
print(new) // Prints 100$
answered 52 mins ago
user2531284
8519
8519
add a comment |Â
add a comment |Â
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)")
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
add a comment |Â
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)")
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
add a comment |Â
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)")
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)")
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password