如何在Swift中使用名称为Objective-C的类作为关键字 [英] How to use Objective-C classes with names which are keywords in Swift
问题描述
我们正在尝试使用BZObjectStore库( https://github.com/expensivegasprices/BZObjectStore )在我们的Swift项目中.
We are trying to use the BZObjectStore library (https://github.com/expensivegasprices/BZObjectStore) in our Swift project.
但是在该库中,它们使用"where"作为名称来设置查询条件.
But in this library, they use 'where' as a name to set the query conditions.
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中的代码.那么有什么办法可以解决这个问题?
But unfortunately, 'where' is a keyword in Swift. We do not want to change the code in BZObjectStore manually. So is there any way we can resolve this ?
推荐答案
您必须使用反引号:
fetchCondition.sqlite.`where` = @"name = 'sample1' and price > 50";
要将保留字用作标识符,请在其前后加上反引号(`).例如,class不是有效的标识符,但是`class`是有效的.反引号不视为标识符的一部分; x和x具有相同的含义.
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.
这篇关于如何在Swift中使用名称为Objective-C的类作为关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!