代码之家  ›  专栏  ›  技术社区  ›  Greg

我可以在ns_swift_名称中使用保留关键字吗?

  •  1
  • Greg  · 技术社区  · 8 年前

    我试图使objective-c委托协议更适合在swift中使用,并且我遇到了一些问题,无法确定如何使用 NS_SWIFT_NAME .可以用吗 斯威夫特姓名 在swift名称中指定与objective-c关键字同名的参数(特别是 for in )是吗?

    这不会生成:

    @protocol MyContainerDelegate
      -(NSInteger)myContainerContentsCount:(MyContainer *)container
        NS_SWIFT_NAME(contentsCount(in container:));
    @end
    

    但如果我宣布 NS_SWIFT_NAME(contentsCount(inContainer:)) 它起作用了。

    我很肯定我见过快速的方法 对于 在里面 对于它们的外部参数名,但我不知道在哪里可以找到这样的示例定义。这能用吗 斯威夫特姓名 是吗?

    1 回复  |  直到 8 年前
        1
  •  3
  •   Martin R    8 年前

    问题不在于关键字,而在于您同时指定了两个参数 中的标签和参数名 NS_SWIFT_NAME 是的。参数标签 已经足够了。与

    @protocol MyContainerDelegate
    -(NSInteger)myContainerContentsCount:(MyContainer *)container
        NS_SWIFT_NAME(contentsCount(in:));
    @end
    

    协议作为

    public protocol MyContainerDelegate {
        public func contentsCount(in container: MyContainer!) -> Int
    }