swift error: Use of 'centralManager' refers to instance method rather than var 'centralManager' in module

醉酒当歌 提交于 2021-02-11 13:10:17

问题


I have an Obj-C app that imports an Obj-C static lib which contains a .swift file with func run_central().

I can't get the app to reference run_central() and get the build error.

Here is Obj-C app project and its imported Obj-C static library Hub_lib that also contains (ie. imports) the .swift file....

ObjC app project has this file........

ViewController.mm  .....................     (is .mm because it imports C++ lib)

. . .


- (IBAction)run_simple_central:(id)sender 
{
    Hub_lib* hub_lib = [Hub_lib new];
    [hub_lib run_central]; <<<<<<<<<<<<<  No visible @interface for Hub_lib' declares the selector 'run_central'
}

@end


ObjC static library Hub_lib project has these files........  

Hub_lib-Bridging-Header.h  ............................  <<<<<<<<<<<<<<<<<<   EMPTY -- NO CODE
//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

BLE.swift file ..........................

import UIKit
import CoreBluetooth
import os

var centralManager: CBCentralManager?

class BLE_Central: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate
{
. . .
    public func run_central()   // <<<<<<<<<<<<<<<<<<<  THE FUNCTION
    {
        Hub_lib.centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionShowPowerAlertKey: true])
    }

回答1:


The access control keyword public in swift does not make your func run_central() available to objective-c in a project as is. It just gives you control of the access level of your function in the swift module.
docs.swift.org - Access Levels for Frameworks

When you develop a framework, mark the public-facing interface to that framework as open or public so that it can be viewed and accessed by other modules, such as an app that imports the framework.

When used in projects targeting a frameworks it (public) should publish the function in its generated header file. Apple Docs - Import Code Within a Framework Target

Following the instructions for bridging to Objective-C,
to make your function available you need to expose it via objc

@objc public func run_central() { /* ... */ }

This should allow you to call your NSObject inherited swift class BLE_Central from within Objective-C via

BLE_Central *ble = [BLE_Central new];
[ble run_central];

Now the difficulties come up because class BLE_Central is not public nor is it exposed from swift via objc in a project that has no knowledge of a header declaring this. Leading to the clue you have to

#import <Hub_lib/Hub_lib-Swift.h>

to declare its content in your project that makes use of your framework or module. Where the naming convention say the import rule should look like ...

#import <ProductName/ProductModuleName-Swift.h>

PS: while objc exposes a function or class to objective-c in the same project,
nonobjc does the opposite.

The nonobjc attribute tells the compiler to make the declaration unavailable in Objective-C code, even though it’s possible to represent it in Objective-C.



来源:https://stackoverflow.com/questions/64904510/swift-error-use-of-centralmanager-refers-to-instance-method-rather-than-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!