如何将 Objective-C 类 与 Swift 中的关键字名称一起使用

How to use Objective-C classes with names which are keywords in Swift

我们正在尝试在我们的 Swift 项目中使用 BZObjectStore 库 (https://github.com/expensivegasprices/BZObjectStore)。

但是在这个库中,他们使用'where'作为名称来设置查询条件。

BZObjectStoreConditionModel *fetchCondition = [BZObjectStoreConditionModel condition];
fetchCondition.sqlite.where = @"name = 'sample1' and price > 50";
fetchCondition.sqlite.orderBy = @"name desc";

NSArray *objects = [os fetchObjects:[SampleModel class] condition:fetchCondition error:&error];

但不幸的是,'where' 是 Swift 中的关键字。我们不想手动更改 BZObjectStore 中的代码。那么我们有什么办法可以解决这个问题吗?

你必须使用反引号:

fetchCondition.sqlite.`where` = @"name = 'sample1' and price > 50";

To use a reserved word as an identifier, put a backtick (`) before and after it. For example, class is not a valid identifier, but `class` is valid. The backticks are not considered part of the identifier; `x` and x have the same meaning.

The Swift Programming Language – Lexical Structure