xcode 如何使用Objective C类中的多个参数调用Swift函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25887096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call Swift function with more than one parameter from Objective C class?
提问by Arnlee Vizcayno
For example I have this method in swift:
例如,我在 swift 中有这个方法:
@objc class MyClass: NSObject
....
@objc class func viewWithIndex(index: Int, str: String) {
println(index, str)
}
then i wanna call that method in my objective-c class, and i was expecting as simple as this call [MyClass viewWithIndex:10 str:@"string"];
but it doesn't work.
然后我想在我的objective-c 类中调用该方法,我期望像这个调用一样简单,[MyClass viewWithIndex:10 str:@"string"];
但它不起作用。
How do i call it? Please help.
我怎么称呼它?请帮忙。
Note: I have already a working swift function call to objective-c [MyClass showSomething];
so that means i have successfully setup necessary settings to bridge classes. Only the function that has two of more parameters is my problem. :)
注意:我已经有一个对objective-c的快速函数调用,[MyClass showSomething];
这意味着我已经成功地设置了桥接类的必要设置。只有具有两个以上参数的函数才是我的问题。:)
Solved:
解决了:
I dont know what i happened but i just restarted my mac and removed objc
and it worked with the call [MyClass viewWithIndex:10 str:@"string"];
. I remember reading in documentation.
我不知道我发生了什么,但我只是重新启动了我的 mac 并删除了objc
它,它可以与电话一起工作[MyClass viewWithIndex:10 str:@"string"];
。我记得在文档中阅读过。
Migrating Your Objective-C Code to Swift
- To be accessible and usable in Objective-C, a Swift class must be a descendant of an Objective-C class or it must be marked @objc.
- 要在 Objective-C 中访问和使用,Swift 类必须是 Objective-C 类的后代,或者必须标记为 @objc。
回答by haplo1384
This worked for me in Swift 3.0
这在 Swift 3.0 中对我有用
public class func viewWithIndex(_ index: Int, str: String) {
println(index, str)
}
Adding the underscore before the first parameter in the Swift declaration allowed me to call from objective c without naming the first parameter, like this:
在 Swift 声明中的第一个参数之前添加下划线允许我从目标 c 调用而无需命名第一个参数,如下所示:
[MyClass viewWithIndex:10 str:@"string"]
回答by Jasper Blues
I believe you'll need to mark the function as either public (makes sense) or dynamic. Otherwise it will be a candidate for Swift's optimization (inline or vtable the method) which will make it invisible to Objective-C.
我相信您需要将该功能标记为公共(有意义)或动态。否则它将成为 Swift 优化(内联或 vtable 方法)的候选者,这将使其对 Objective-C 不可见。
Try this:
尝试这个:
public class func viewWithIndex(index: Int, str: String) {
println(index, str)
}
Or this:(doesn't really make sense, but should also work)
或者这个:(实际上没有意义,但也应该有效)
private dynamic class func viewWithIndex(index: Int, str: String) {
println(index, str)
}
回答by vinay kumar
forward declaration of your class in .h file of objective-c
在objective-c的.h文件中预先声明你的类
@class <MySwiftClass>
import "Productname-swift.h" in obj-c .m class use
在 obj-c .m 类中导入“Productname-swift.h”使用
[SwiftClass storeWithData:@"Hi" password:@"secret"];
SwiftClass.swift
SwiftClass.swift
@objcMembers class SwiftClass{
public class func store(data: String,password:String)->Bool{
let saveSuccessful: Bool = KeychainWrapper.standard.set(data, forKey: password)
return saveSuccessful;
}
}