[英]How to put the of first and last element of Array in Swift
我有這樣的字典
["Price": ["$00.00 - $200.00", "$200.00 - $400.00", "$600.00 - $800.00"]]
現在我將所有字典值存儲在這樣的數組中
var priceRange: [String] = [String]()
if let obj = currentFilters["Price"] as? [String] {
self.priceRange = obj
printD(self.priceRange)
}
通過使用Array.first
和Array.last
方法,我將獲得我的數組的第一個元素和最后一個元素的值。
let first = priceRange.first ?? "" // will get("[$00.00 - $200.00]")
let last = priceRange.last ?? "" // will get("[$600.00 - $800.00]")
但我真正想要的是我想從第一從去年的$ 00.00和$ 800至使所需組合[$點- $ 800.00。
我怎樣才能做到這一點。 請幫忙?
您需要取first
值( "$00.00 - $200.00"
),然后取last
值( "$600.00 - $800.00"
),然后用“ -
”符號拆分它們,分別取第一個和最后一個值並將它組合成單個字符串。
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)"
為了更好的選項處理你可以使用它( 注意 ,我正在遵循我的描述性編碼風格。這段代碼可以更加緊湊)
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"])
過去上面的代碼到操場。 它可以用更短的方式編寫,但為了描述,我保持這樣
您可以執行以下操作:
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: " - ")
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"
]
))
您可以使用split
來基於-
字符拆分范圍,而不是刪除空格。
let first = priceRange.first?.split(separator: "-").first?.trimmingCharacters(in: .whitespaces) ?? ""
let last = priceRange.last?.split(separator: "-").last?.trimmingCharacters(in: .whitespaces) ?? ""
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)]")
}
使用此您可以找到您的值:
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
}
//使用組件(separatedBy :):
讓price = f.components(separatedBy:“ - ”)
price.first // $ 600.00
prices.last // $ 800.00
您還可以使用String.components(separatedBy :):
var text = "100$ - 200$"
var new = text.components(separatedBy: " - ")[0]
print(new) // Prints 100$
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.