该错误是指您的函数签名getTimeStamps
, String -> String
, 具有返回类型String
, 但您返回的值query[t_time_stamp]
, 是一个Expression<String?>
.
Query
结构可以用Expression
s 下标以返回表达式的命名空间版本:
let id = Expression<Int64>("id") // literally: "id"
let users = db["users"]
let users_id = users[id] // literally: "users"."id"
在您的情况下,下标query
只是t_time_stamp
返回t_time_stamp
表达式的新命名空间版本(在您的版本中,"time_stamps"."TIME_StAMP"
)。这有助于消歧,但不太可能是您的意图。
很难从提供的代码中准确判断出您希望从函数返回什么,但看起来您想要执行查询以提取值。Row
结构一旦被提取,就可以用表达式下标以检索基础值。
如果您要检索单行,请尝试以下操作:
if let row = time_stamps.filter(like(tablename, t_tabelle)).first {
return row[t_time_stamp]
}
但是,您的函数仍然返回String
,而不是String?
。如果您的查询有可能返回零行或它返回的任何行都具有NULL
时间戳列,则您需要相应地处理您的选项。
但是,如果NULL
时间戳表明存在编程错误/错误,则应相应更新String?
为String
:
let t_time_stamp = Expression<String>["TIME_StAMP"]
// ...
return query.first![t_time_stamp]
请注意,如果您对可选值的可能性处理不当,上述内容将会崩溃。