从 ObjC 访问函数到 Swift 包的正确语法是什么?
What is the proper syntax for accessing a function from ObjC to a Swift Package?
简单场景: 我想从 Objective-C.
中调用任何 Swift 包成员函数
我正在构建一个 reference/demo/proof-of-concept 应用程序以将 ObjC 与 Swift 混合使用。
这是 Swift 方面的 有效 片段:
import Foundation
import RicPackage
final class Greetings: NSObject {
@objc public static let shared = Greetings()
private override init() {}
@objc public func doSomething(name: String) {
print("Do something, \(name)")
let ricClass = RicClass()
ricClass.sayHello()
}
@objc public func hello() {
print("Hello Everybody!")
}
}
这是Objective-C方面:
#import "ViewController.h"
#import "ObjcSwiftHeader-Swift.h"
@class RicClass;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"Inside VC ViewDidLoad.");
Greetings *greetings = [Greetings shared];
[greetings hello];
[greetings doSomethingWithName: @"Ric"];
RicClass *ricClass = [RicClass alloc];
}
@end
我遇到了这个编译器错误:
这是项目的快照:
这是 Swift 包裹的东西:
import Foundation
public struct RicStruct {
public private(set) var text = "Hello, World!"
public init() {
}
public func sayHello() -> String {
"Hello Ric!"
}
}
public class RicClass: NSObject {
@objc public var msg = "Mother has a feeling, I might be too appealing."
@objc public let text = "Hello Everybody!"
public override init() {}
public func sayHello() {
print(text)
}
public func doSomething() {
print("Inside doSomething()")
}
}
必须有一个简单的解决方案....
包作为动态嵌入式框架链接到您的应用程序中,要使用它们,您应该将包的库导入源文件:
Swift:
import RicPackage
Objective-C:
@import RicPackage;
简单场景: 我想从 Objective-C.
中调用任何 Swift 包成员函数我正在构建一个 reference/demo/proof-of-concept 应用程序以将 ObjC 与 Swift 混合使用。
这是 Swift 方面的 有效 片段:
import Foundation
import RicPackage
final class Greetings: NSObject {
@objc public static let shared = Greetings()
private override init() {}
@objc public func doSomething(name: String) {
print("Do something, \(name)")
let ricClass = RicClass()
ricClass.sayHello()
}
@objc public func hello() {
print("Hello Everybody!")
}
}
这是Objective-C方面:
#import "ViewController.h"
#import "ObjcSwiftHeader-Swift.h"
@class RicClass;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"Inside VC ViewDidLoad.");
Greetings *greetings = [Greetings shared];
[greetings hello];
[greetings doSomethingWithName: @"Ric"];
RicClass *ricClass = [RicClass alloc];
}
@end
我遇到了这个编译器错误:
这是项目的快照:
这是 Swift 包裹的东西:
import Foundation
public struct RicStruct {
public private(set) var text = "Hello, World!"
public init() {
}
public func sayHello() -> String {
"Hello Ric!"
}
}
public class RicClass: NSObject {
@objc public var msg = "Mother has a feeling, I might be too appealing."
@objc public let text = "Hello Everybody!"
public override init() {}
public func sayHello() {
print(text)
}
public func doSomething() {
print("Inside doSomething()")
}
}
必须有一个简单的解决方案....
包作为动态嵌入式框架链接到您的应用程序中,要使用它们,您应该将包的库导入源文件:
Swift:
import RicPackage
Objective-C:
@import RicPackage;