Skip to main content
 首页 » 操作系统

objective-c之如何调用 xxxxWithClass :(class) in swift

2024年01月01日56tuyile006

我想在 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) 

怎么调用它?

请您参考如下方法:

来自 Swift 文档:

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.

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)