是否有可能在艺术家变量之前进行分配在where子条款中使用?
var artist
switch fullline {
case let path where path.hasPrefix("Monet"):
artist = "Monet"
case let path where path.hasPrefix("Cezanne"):
artist = "Cezanne"
default: ()
}
关闭:
case let path where { () -> Bool in let artist = "Monet"; return path.hasPrefix(artist) }:
错误:
() - > Bool'不能转换为'Bool'
上下文
我有自由形式的文本行,艺术家名称作为所需的前缀 按摩以输出一致的人类可读文本。 e.g。
Monet : Snow at Argenteuil 02, 1874 Monet - Snow at Argenteuil, 1874, 3rd Floor Collections Monet, Claude - 1875, Snow in Argenteuil Cezzane - Vase of Flowers, 1880-81, print Cezzane, Paul 1900-1903 Vase of Flowers Cezzane - Vase with Flowers, 1895-1896
将会有一个代码片段执行详细的处理/分类 为每个艺术家。因此,处理逻辑依赖于艺术家。
我想定义类似于以下构造
switch fullline
hasPrefix(artist = "Monet")
-> code logic 1
get_birthday(artist)
hasPrefix(artist = "Cezzane")
-> code logic 2
get_birthday(artist)
答案 0 :(得分:3)
通过对亚历山大的结构进行一些修改,你可以写下这样的东西:
struct PrefixMatcherWithHandler {
var handler: (String)->Void
var string: String
init(_ string: String, handler: @escaping (String)->Void) {
self.string = string
self.handler = handler
}
static func ~= (prefix: String, matcher: PrefixMatcherWithHandler) -> Bool {
if matcher.string.hasPrefix(prefix) {
matcher.handler(prefix)
return true
} else {
return false
}
}
}
var fullline: String = "Monet, Claude"
var artist: String? = nil
let matcher = PrefixMatcherWithHandler(fullline) {str in
artist = str
}
switch matcher {
case "Monet":
break
case "Cezanne":
break
default: break
}
print(artist ?? "") //->Monet
但是在~=
这样的布尔运算符中产生一些副作用会使代码的可读性降低,并且很容易产生意外的结果。
如果您只想减少对同一事物的冗余引用,switch
- 语句可能不是一个好的工具。
例如,您可以在不定义特定匹配器类型的情况下获得相同的结果:
var fullline: String = "Monet, Claude"
var artist: String? = nil
if let match = ["Monet", "Cezanne"].first(where: {fullline.hasPrefix($0)}) {
artist = match
}
print(artist ?? "") //->Monet
对于问题的更新部分,已添加
以下代码与前缀匹配略有不同,但我相信您不希望"Mon"
与行Monet, Claude - 1875, Snow in Argenteuil
匹配。
extension String {
var firstWord: String? {
var result: String? = nil
enumerateSubstrings(in: startIndex..<endIndex, options: .byWords) {str, _, _, stop in
result = str
stop = true
}
return result
}
}
func get_birthday(_ artist: String) {
//What do you want to do?
print(artist)
}
var fullline: String = "Monet, Claude - 1875, Snow in Argenteuil"
switch fullline.firstWord {
case let artist? where artist == "Monet":
//code dedicated for "Monet"
get_birthday(artist)
case let artist? where artist == "Cezanne":
//code dedicated for "Cezanne"
get_birthday(artist)
default:
break
}
当您可以检索适合switch
- 语句的数据时,代码将更加直观和可读。
答案 1 :(得分:1)
你给了那个预期布尔值的闭包。不确定为什么要这样做,但是你可以通过使用 ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.spinner_hint_item,
???);
来调用闭包来使它工作。
()
我将如何做到这一点:
var artist
switch fullline {
case let path where { () -> Bool in let artist = "Monet"; return path.hasPrefix(artist) }():
artist = "Monet"
case let path where path.hasPrefix("Cezanne"):
artist = "Cezanne"
default: ()
}
更一般的解决方案:
import Foundation
struct PrefixMatcher {
let string: String
init(_ string: String) { self.string = string }
static func ~= (prefix: String, matcher: PrefixMatcher) -> Bool {
return matcher.string.hasPrefix(prefix)
}
}
extension String {
var prefix: PrefixMatcher { return PrefixMatcher(self) }
}
let fullline = "Monet 123456789"
let artist: String?
switch fullline.prefix {
case "Monet": artist = "Monet"
case "Cezanne": artist = "Cezanne"
default: artist = nil
}
print(artist as Any)
答案 2 :(得分:0)
您可以通过切换一个枚举元组和可选元组来实现。 可选的也是一个枚举,因此您可以同时切换两者
enum SomeSnum {
case a, b, c
}
let someString: String? = "something"
let esomeEnum = SomeSnum.b
switch(esomeEnum, someString) {
case (.b, .some(let unwrappedSomething)) where unwrappedSomething.hasPrefix("so"):
print("case .b, \(unwrappedSomething) is unwrapped, and it has `so` prefix")
case (.a, .none):
print("case .a, and optional is nil")
default:
print("Something else")
}
您也可以执行if语句
if case let (.b, .some(unwrappedSomething)) = (esomeEnum, someString), unwrappedSomething.hasPrefix("so") {
} else if case (.a, .none) = (esomeEnum, someString) {
} else {
}