如何使用名称是Swift中关键字的Objective-C类

Sha*_*eng 1 naming objective-c 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];
Run Code Online (Sandbox Code Playgroud)

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

idm*_*ean 5

你必须使用反引号:

fetchCondition.sqlite.`where` = @"name = 'sample1' and price > 50";
Run Code Online (Sandbox Code Playgroud)

要使用保留字作为标识符,请在其前后放置一个反引号(`).例如,class不是有效的标识符,但`class`是有效的.反引号不被视为标识符的一部分; `x`和x具有相同的含义.

Swift编程语言 - 词汇结构