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

流过昼夜 提交于 2019-12-01 09:47:29

问题


We are trying to use the BZObjectStore library (https://github.com/expensivegasprices/BZObjectStore) in our Swift project.

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];

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 ?


回答1:


You have to use backticks:

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



来源:https://stackoverflow.com/questions/31717511/how-to-use-objective-c-classes-with-names-which-are-keywords-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!