I am creating a swift package which needs to use some old Objective-C code that I am not ready to port in Swift right now.
This is my package structure:
- MyPackage
|
|-README.md
|
|-Package.swift
|
|-Sources
|
|-MyPackageSwift
| |
| |- MyPackage.swift
|
|-MyPackageObj
|
|-Include
| |
| |- MyObjCClass.h
|
|- MyObjCClass.m
This is the Package.swift:
import PackageDescription
let package = Package(
name: "MyPackage",
products: [
.library(
name: "MyPackage",
targets: ["MyPackageSwift","MyPackageObj"]),
],
dependencies: [
],
targets: [
.target(
name: "MyPackageSwift"
),
.target(
name: "MyPackageObj"
),
]
)
The Obj-C class is:
//
// MyObjCClass.h
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyObjCClass : NSObject
- (NSString *)myObjCService;
@end
NS_ASSUME_NONNULL_END
and:
//
// MyObjCClass.m
//
#import "MyObjCClass.h"
@implementation MyObjCClass
- (NSString *)myObjCService {
return @"This is myObjCService";
}
@end
My problem is that since I cannot use the bridge file I don't know how to make the Objective-C class visible from the Swift code:
public struct MyPackage {
public init() {
}
public func myService() -> String {
let objcService = MyObjCClass()
return "This is myService in MyPackage"
}
}
and I get this error:
Cannot find 'MyObjCClass' in scope
I eventually found the way to make it working. These are the required changes:
...
.target(
name: "MyPackageSwift",
dependencies: ["MyPackageObj"] <-----
),
...
import MyPackageObj <-----
public struct MyPackage {
public init() {
}
public func myService() -> String {
let objcService = MyObjCClass()
return "This is myService in MyPackage " + objcService.myObjCService()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With