swift 错误:使用“centralManager"是指实例方法而不是模块中的 var“centralManager" [英] swift error: Use of 'centralManager' refers to instance method rather than var 'centralManager' in module

查看:25
本文介绍了swift 错误:使用“centralManager"是指实例方法而不是模块中的 var“centralManager"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Obj-C 应用程序,它导入一个 Obj-C 静态库,其中包含一个带有 func run_central() 的 .swift 文件.

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

我无法让应用引用 run_central() 并出现构建错误.

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

这是 Obj-C 应用程序项目及其导入的 Obj-C 静态库 Hub_lib,它还包含(即导入).swift 文件....

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])
    }

推荐答案

swift 中的访问控制关键字 public 不会使您的 func run_central() 可用于目标 -c 在一个项目中.它只是让您控制 swift 模块中函数的访问级别.
docs.swift.org - 框架的访问级别

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.

在以框架为目标的项目中使用时,它 (public) 应该在其生成的头文件中发布函数.Apple 文档 - 在框架目标内导入代码

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

按照桥接Objective-C的说明进行操作,
要使您的函数可用,您需要通过 objc

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

这应该允许您通过

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

现在困难出现了,因为 class BLE_Central 不是 public 也不是在没有知识的项目中通过 objc 从 swift 公开声明这一点的标题.导致你必须要的线索

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:虽然 objc 向同一项目中的 Objective-c 公开了一个函数或类,
nonobjc 会执行 相反.

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

nonobjc 属性告诉编译器使声明在 Objective-C 代码中不可用,即使它可以在 Objective-C 中表示.

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.

这篇关于swift 错误:使用“centralManager"是指实例方法而不是模块中的 var“centralManager"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆