【问题标题】:swift build errors from Objective-C app due to import of Objective-C static library that contains a .swift file由于导入了包含 .swift 文件的 Objective-C 静态库,来自 Objective-C 应用程序的快速构建错误
【发布时间】:2021-03-16 18:48:23
【问题描述】:

我正在将一个 ObjC 静态库导入到我的 ObjC 应用程序中,但出现 95 个与 swift 相关的构建错误。
ObjC 静态库构建正常,包含 .m .h 和 .swift 源文件。
Swift 文件导入 CoreBluetooth。

以下内容...

  1. 构建错误
  2. 源代码文本
  3. 项目压缩包,应 Asperi 的友好请求
    更新 2
    更新 3

..1.这里 Xcode 构建错误加上必要的源代码............

..2.源码正文

//  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")
    }

. . .   

更新 1 -- 2020 年 12 月 5 日...

  1. 项目压缩包projects: sim_backend_UI & mobile_sensor_API

我从 mac OS 压缩了两个项目 sim_backend_UI 和 mobile_sensor_API。

您需要使用您的... 目标 > 签约 > 团队 和 TARGETS > Build Settings > All > Packaging > Product Bundle Identifier

..因为我从他们那里删除了我们的公司。

更新 2 - 十二月。 6 条已删除

更新 3 - 十二月。 7 回答了 1

我从 Asperi 获得答案 1 的步骤...

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。将 mobile_sensor_API.h 拖入 app proj nav e.将 Xcode “活动方案”设置为应用项目和设备

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

【问题讨论】:

  • 我认为这不是代码,而是项目设置问题。您会以某种方式授予对项目的访问权限吗?
  • Asperi:我刚刚添加了对项目的访问权限。从此链接下载项目:drive.google.com/file/d/1tefgnZf0b5ulpwI2lMb9ly8m9PO9Pbfh/…
  • Asperi:我从 mac OS 压缩了两个项目 sim_backend_UI 和 mobile_sensor_API。您将需要使用您的... 目标 > 签名 > 团队和目标 > 构建设置 > 全部 > 打包 > 产品捆绑标识符来设置项目 ..因为我从中删除了我们的公司。

标签: ios objective-c swift xcode language-interoperability


【解决方案1】:

最简单的解决方案是让 Xcode 生成所有依赖项并包含所有必需的系统库。

这是我所做的:

  1. 在顶级文件夹级别创建工作区

  2. 将 mobile_sensor_API.xcodeproj 添加到工作区

  3. 将 sim_backend_UI.xcodeproj 添加到工作区

  4. 通过工作区添加 sim_backend_UI 对 lib 的依赖

  1. 添加Some.swift(任何你想要的swift文件)到sim_backend_UI,只是为了Xcode添加所需的系统swift动态库(静态库中的swift部分也需要)......并确认在出现对话框

  1. 构建 >>> 成功!

更新 3 ..................................在我通过第 6 步完成此答案后,我得到了“成功”,但 sim_backend_UI.app 为 RED,无法在 iphone 上运行。

【讨论】:

  • 太棒了!迫不及待想试试。所以,听起来我的大错误是没有工作空间。
  • 试图让应用依赖于 lib,我将 lib 的 .a 拖到应用项目 > 目标 > 常规 > 框架、库和嵌入式内容,已构建 (cmd-B),已构建 lib 但应用的产品 sim_backend_UI .app 仍然是红色的,但标题栏显示“成功”。 (上面是我所有步骤的更新)
  • 你错过了我的步骤...不要拖动(此外从 Finder 中删除制作的库) - 只需按照步骤 4 中的指定添加依赖项 - Xcode 将在需要时自行构建(重建)库.
  • Asperi,要执行第 4 步“添加依赖项”,我将 libmobile_sensor_API.a 从 lib 的产品拖到应用 > 目标 > 框架、库和嵌入式内容中。这是正确的方法吗?
  • mobile_sensor_API.h 文件呢?这需要拖到应用项目中吗?
猜你喜欢
  • 1970-01-01
  • 2011-10-16
  • 2017-07-12
  • 1970-01-01
  • 2015-10-23
  • 2016-05-28
  • 2012-02-15
  • 1970-01-01
  • 2017-05-23
相关资源
最近更新 更多