【问题标题】:Make A Call In Objective-C To A Method Using Swift String使用 Swift 字符串在 Objective-C 中调用方法
【发布时间】:2016-06-23 19:19:29
【问题描述】:

我在 Swift 中有一个类方法:

import Foundation

public class MyClass : NSObject{
    class func registerDevice(userId uId: String, deviceId dId: String, pushServiceToken token: String) -> Void{ /*...*? }
}

然后我试图从 Objective-C 调用该函数,但我正在做一些事情。这是我正在尝试的:

[MyClass registerDevice: @"user id" deviceId: [UIDevice currentDevice].identifierForVendor.UUIDString pushServiceToken: registrationToken];

...但我得到了错误:

"没有已知的选择器'registerDevice::deviceId:pushServiceToken:'的类方法"

拨打这个电话的正确方法是什么?

【问题讨论】:

  • 不,是,因为您使用了错误的选择器。这是“方法名称”,无论其参数类型如何。可能您必须在registerDevice 之后添加UserId 左右。

标签: ios objective-c swift


【解决方案1】:

您可以通过检查自动生成的<module>-Swift.h 文件来了解 Xcode 如何将 Swift 签名转换为 Objective-C。 <module> 将是应用程序的名称,如果您尚未定义模块(和/或您未构建框架)。

要查找文件(它不会出现在您的项目源中),请点击 cmd-shift-O 并输入 -Swift.h

编辑:在这种情况下,该方法被命名为“registerDeviceWithUserId”。

【讨论】:

  • 很好的答案,不仅可以为我指明正确的方向,还可以为键盘快捷键提供方便的提示!事实证明,该函数被重命名为“registerDeviceWithUserId”。
猜你喜欢
  • 2012-06-22
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-09
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多