简体   繁体   English

Objective-C 项目中的 Swift CocoaPods 库

[英]Swift CocoaPods Library in Objective-C Project

I have found several online resources on how to include an Objective-C library into a Swift project and a few limited resources on how to do the reverse (which is what I'm after).我找到了一些关于如何将 Objective-C 库包含到 Swift 项目中的在线资源,以及一些关于如何反向操作的有限资源(这就是我所追求的)。

I have managed to get my project to compile and run based on the work I did here in this question: Swift CocoaPod Library in Objective-C Project Migration from Swift 3 to 4/5根据我在此问题中所做的工作,我设法让我的项目编译和运行: Objective-C 项目迁移中的 Swift CocoaPod Library from Swift 3 to 4/5

However whenever I try to access anything from the Swift library in my ObjC project my app crashes with the following...但是,每当我尝试从 ObjC 项目中的 Swift 库访问任何内容时,我的应用程序都会崩溃,并显示以下内容...

2020-01-29 14:42:09.756352-0700 Hyperion[13547:2723315] -[HGCircularSlider.RangeCircularSlider setStartPointValue:]: unrecognized selector sent to instance 0x1074088c0
2020-01-29 14:42:09.763045-0700 Hyperion[13547:2723315] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HGCircularSlider.RangeCircularSlider setStartPointValue:]: unrecognized selector sent to instance 0x1074088c0'
*** First throw call stack:
(0x18e32ea48 0x18e055fa4 0x18e2325a8 0x1923cf86c 0x18e332af4 0x18e334a7c 0x10208f554 0x10208d4d4 0x192d15dec 0x192be580c 0x10208d388 0x10208b570 0x191d8cab8 0x191d8d160 0x191d11b40 0x191d11c6c 0x191d192c0 0x191d23b84 0x191d23f70 0x191d1716c 0x191d119a0 0x191d87134 0x191d87838 0x1924f3d74 0x1924f7224 0x1924f70d0 0x1924f739c 0x191d87290 0x191d87838 0x1924f3d74 0x1924f7224 0x1924f70d0 0x1924f739c 0x191d87290 0x191d87838 0x191cceaec 0x191ccdf08 0x191cca9c8 0x191cca7e0 0x191ccde54 0x1923a2e50 0x191b3e148 0x1923a2e50 0x191dd9494 0x191dd97f8 0x191b40b58 0x1923a2e50 0x191dd9494 0x191dd97f8 0x191dd8814 0x1923dc3d4 0x1923dd714 0x1923b9e2c 0x192431fa4 0x192434500 0x19242d374 0x18e2aca00 0x18e2ac958 0x18e2ac0f0 0x18e2a723c 0x18e2a6adc 0x19822c328 0x1923a1ae0 0x1020de520 0x18e130360)
libc++abi.dylib: terminating with uncaught exception of type NSException

As I state in the answer of my question linked above I have written a Swift class that exposes the CocoaPod's library functions I need which allows my project to compile...正如我在上面链接的问题的答案中所述,我编写了一个 Swift 类,该类公开了我需要的 CocoaPod 库函数,它允许我的项目编译...

import Foundation
import HGCircularSlider

@objc public class CircularSliderObjc: RangeCircularSlider
{
    @objc override open var startPointValue: CGFloat {

        get {
            return super.startPointValue;
        }

        set {
            super.startPointValue = newValue;
        }
    }

    @objc override open var endPointValue: CGFloat {

        get {
            return super.endPointValue;
        }

        set {
            super.endPointValue = newValue;
        }
    }

    @objc override open var endThumbImage: UIImage? {

        get {
            return super.endThumbImage;
        }

        set {
            super.endThumbImage = newValue;
        }
    }

    @objc override open var startThumbImage: UIImage? {

        get {
            return super.startThumbImage;
        }

        set {
            super.startThumbImage = newValue;
        }
    }
}

As you can see in the project I have exposed startPointValue as both a set and a get.正如您在项目中看到的,我将 startPointValue 公开为集合和获取。 However invoking this function at runtime causes the crash.但是在运行时调用此函数会导致崩溃。

Here is the calling class's header...这是调用类的标题...

#import "BaseViewController.h"
#import "Hyperion-Swift.h"

@interface TemperatureDeviceViewController : BaseViewController

@property (weak, nonatomic) IBOutlet CircularSliderObjc *rangeSlider;

And the call in my .m file...我的 .m 文件中的调用...

[self.rangeSlider setStartPointValue:[self getSliderValueForTemp:startValue]];

I'm assuming I'm missing one critical step here?我假设我在这里错过了一个关键步骤?

UPDATE #1更新 #1

I've tried the answer suggested by Max which makes a lot of sense.我已经尝试过 Max 建议的答案,这很有意义。 However because this is an IBOutlet it in my ViewController his solution really doesn't work for my use case here.然而,因为这是一个 IBOutlet,它在我的 ViewController 中,他的解决方案真的不适用于我的用例。

I did some further debugging and put breakpoints all over my startPointValue function in order to see what the "super" was referencing.我做了一些进一步的调试,并在我的 startPointValue 函数中放置了断点,以查看“超级”所引用的内容。 However none of the breakpoints are triggered before the crash happens.然而,在崩溃发生之前没有触发任何断点。 Therefore I'm not sure that the call to "super" is the actual cause?因此,我不确定对“超级”的调用是真正的原因吗? Verdict is still out on this one I think.我认为这个结论仍然存在。

Try redesigning your wrapper class to encapsulate rather than inherit from the framework class.尝试重新设计您的包装类以封装而不是从框架类继承 For example:例如:

@objc public class CircularSliderObjc
{
    let slider: RangeCircularSlider // NOT @objc!
    @objc var startPointValue: CGFloat {
        get {
            return slider.startPointValue;
        }
        set {
            slider.startPointValue = newValue;
        }
    }

That way Objective-C doesn't even need to know that the Swift class exists at all.这样,Objective-C 甚至根本不需要知道 Swift 类的存在。 My guess is that this is crashing on the super , where something Swift-specific is being accessed from Objective-C.我的猜测是这会在super上崩溃,在那里可以从 Objective-C 访问特定于 Swift 的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM