我有这样的字典
["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 [$ 00.00-$ 800.00] 的期望组合。
我该怎么做。请帮忙?
答案 0 :(得分:4)
您需要先获取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)"
为更好地处理可选内容,您可以使用此代码( NB ,我遵循的是过度描述的编码风格。该代码可以紧凑得多)
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"])
将上面的代码粘贴到操场上。它可以写得更短一些,但是为了描述的缘故,我还是这样保留它
答案 1 :(得分:2)
您可以执行以下操作:
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: " - ")
答案 2 :(得分:2)
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"
]
))
答案 3 :(得分:1)
您可以使用split
根据-
字符来划分范围,而不要删除空格。
let first = priceRange.first?.split(separator: "-").first?.trimmingCharacters(in: .whitespaces) ?? ""
let last = priceRange.last?.split(separator: "-").last?.trimmingCharacters(in: .whitespaces) ?? ""
答案 4 :(得分:1)
server 10.XXX.X.XX:6380 max_fails=2 fail_timeout=30s backup;
答案 5 :(得分:1)
使用此方法,您可以找到自己的值:
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
}
答案 6 :(得分:0)
//使用组件(separatedBy:):
让价格= f.components(separatedBy:“-”)
prices.first // $ 600.00
prices.last // $ 800.00
答案 7 :(得分:0)
您还可以使用String.components(separatedBy:):
var text = "100$ - 200$"
var new = text.components(separatedBy: " - ")[0]
print(new) // Prints 100$