如何在swift中调用xxxxWithClass :(类)

时间:2014-12-27 16:02:17

标签: objective-c swift restkit

我想在swift中的RestKit RKRoute中调用此方法:

+ (instancetype)routeWithClass:(Class)objectClass pathPattern:(NSString *)pathPattern method:(RKRequestMethod)method;

但我发现我不能使用'class'作为参数标签,编译器将其作为关键字处理

var route:RKRoute = RKRoute(class: Article.self, pathPattern: "/categories/:comment.name/articles/:articleID/comments/", method:RKRequestMethod.GET)

怎么称呼它?

1 个答案:

答案 0 :(得分:3)

来自Swift文档:

  

要使用保留字作为标识符,请先添加反引号(`)   之后。例如,class不是有效的标识符,但是   `class`是有效的。反引号不被视为其中的一部分   标识符; `x`和x具有相同的含义。

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html

因此:

var route:RKRoute = RKRoute(`class`: Article.self, pathPattern: "/categories/:comment.name/articles/:articleID/comments/", method:RKRequestMethod.GET)