iosobjective-cswiftxcodelanguage-interoperability

swift build errors from Objective-C app due to import of Objective-C static library that contains a .swift file


I'm importing an ObjC static lib into my ObjC app but get 95 swift-related build errors.
The ObjC static lib builds ok and contains .m .h and .swift source files.
Swift file imports CoreBluetooth.

CONTENTS BELOW...

  1. build errors
  2. source code text
  3. zip of project, in response to kind request from Asperi
    UPDATE 2
    UPDATE 3

..1. Here Xcode build errors plus essential source code....................

enter image description here

..2. source code text

//  OBJC APP 
//  ViewController.h
//  sim_backend_UI
//
//

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

#import "Mobile_Sensor_API.h"

@interface ViewController : UIViewController

@end





//  OBJC APP
//  ViewController.m
//  sim_backend_UI
//
//

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Mobile_Sensor_API* mobile_sensor_API;

    if( !mobile_sensor_API ) mobile_sensor_API = [[ Mobile_Sensor_API alloc] init];

    // Tell Mobile_Sensor_API library to start BLE central scanning:
    [ mobile_sensor_API   _init_and_test ];
}
@end





//  OBJC STATIC LIB with some Swift
//  mobile_sensor_API.h

#import <Foundation/Foundation.h>


@interface Mobile_Sensor_API : NSObject
    -(int)_init_and_test;
@end









//  OBJC STATIC LIB with some Swift
//  mobile_sensor_API.m
//
//

// ObjC...
#import <UIKit/UIKit.h>
#import <CoreBluetooth/CoreBluetooth.h>

// app...
#import "mobile_sensor_API.h"
#import "mobile_sensor_API-Swift.h"

    //#define BUILD_SIM_MOBILE_HUB              ' builds sim mobile_hub
@interface Mobile_Sensor_API()
    @property BLE_Central*   BLE_central_instance;
@end

@implementation Mobile_Sensor_API
 

// Init Maestro lib at boot time and test interface with backend:
-(int)_init_and_test
{
    // Init access to Swift:

    _BLE_central_instance = [[ BLE_Central alloc] init];
    // Start BLE:
    [ _BLE_central_instance   start_central ];

    return 1;
}
@end





/*
//  OBJC STATIC LIB with some Swift

    Abstract:
    A class to discover, connect, receive notifications and write data to sensor peripherals by using a
    transfer service and characteristic.
*/

//import Foundation

import UIKit
import CoreBluetooth
import os

var centralManager: CBCentralManager = CBCentralManager()


@objc open class BLE_Central
: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
{
    var discoveredPeripheral: CBPeripheral?
    var transferCharacteristic: CBCharacteristic?
    var writeIterationsComplete = 0
    var connectionIterationsComplete = 0

    let defaultIterations = 5     // change this value based on test usecase

    var data = Data()

    
    @objc public func start_central()
    {
        os_log("Central_class: start_central")
        mobile_sensor_API.centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: true])
        os_log("Scanning started")
    }

. . .   

UPDATE 1 -- DEC 5, 2020...

  1. zip of project projects: sim_backend_UI & mobile_sensor_API

I zipped both projects sim_backend_UI & mobile_sensor_API from mac OS.

You will need to set projects with your... TARGETS > Signing > Team and TARGETS > Build Settings > All > Packaging > Product Bundle Identifier

..because I delete our company from them.

UPDATE 2 -- DEC. 6 deleted

UPDATE 3 -- DEC. 7 DID ANSWER 1

MY STEPS FOR ANSWER 1 from Asperi...

1.  Created workspace at top folder
1.1 Copied clean app and lib to top folder
2.  Add mobile_sensor_API.xcodeproj to workspace
3.  Add sim_backend_UI.xcodeproj to workspace
4.  Added dependency of sim_backend_UI to lib via workspace
a.  sim_backend_UI proj > General tab > Frameworks, Libs.. > +
b.  Select libmobile_sensor_API.a
c.  Add.


5.  Add Some.swift (any swift file you want) to sim_backend_UI, just for the purpose Xcode add required system swift dynamic libraries (which will be needed for swift part in static library as well)... and confirm creating bridge in appeared dialog
a.  new file > Swift > Some.swift
b.  Create Bridging Header
c.  Added to Some.swift ...

import Foundation

struct Some{}

d. drag mobile_sensor_API.h into app proj nav e. Set Xcode "active scheme" to app project and device

6.  Build 
 I got "succeeded" and it runs on iphone.

Solution

  • The simplest solution is to allow Xcode to make all dependencies and include all required system libs.

    Here is what I did:

    1. Create workspace at top folder level

    2. Added mobile_sensor_API.xcodeproj to workspace

    3. Added sim_backend_UI.xcodeproj to workspace

    4. Added dependency of sim_backend_UI to lib via workspace

    demo

    1. Add Some.swift (any swift file you want) to sim_backend_UI, just for the purpose Xcode add required system swift dynamic libraries (which will be needed for swift part in static library as well)... and confirm creating bridge in appeared dialog

    demo2

    1. Build >>> Succeeded!

    UPDATE 3 .................................. AFTER I DID THIS ANSWER thru step 6, I got "succeeded" but sim_backend_UI.app is RED and won't run on iphone.

    enter image description here