Cosmos icon indicating copy to clipboard operation
Cosmos copied to clipboard

The Objective-C bridge is outdated since Xcode 10 / Swift 4 and results in Xcode errors

Open funnel20 opened this issue 1 year ago • 1 comments

Please consider submitting the following information (if relevant):

  • Library setup method: CocoaPods
  • Version of the library: 23.0.0
  • Xcode version: 15.4
  • OS version iOS 17.6.1

When embedding the project into my Objective-C project with latest Xcode 15.4 while following the manual, I get a ton of Xcode errors. I have related them to 2 root causes and provide solutions below:

Issue 1 - Cannot find type 'XXX' in scope

The bridge file errors:

CosmosSettingsObjCBridge.swift:38:72 Cannot find type 'CosmosView' in scope
CosmosSettingsObjCBridge.swift:39:26 Cannot find 'StarFillMode' in scope
CosmosSettingsObjCBridge.swift:50:47 Cannot find type 'CosmosView' in scope
etc.

Solution

Add an import to the top of this file:

import Cosmos

Issue 2 - No known class method for selector 'YYY'

Wherever I use CosmosSettingsObjCBridge in my ViewControllers, I get errors like:

ViewController.m:53:31 No known class method for selector 'setFillMode:inCosmosView:'

It appears that the functions in the Bridge file are no longer accessible since Xcode 10 / Swift 4.

Solution

This can be resolved in 2 ways:

  1. Add @objc to all of function declarations, e.g.:
@objc public class func setFillMode(_ value: Int, inCosmosView cosmosView: CosmosView) {
  1. Replace @objc in the class declaration of CosmosSettingsObjCBridge by @objcMembers once, so all methods within the class will be exposed to Objective-C:
@objcMembers public class CosmosSettingsObjCBridge: NSObject {

According to this article, is available since iOS 8.0.

Option 2 is simple and elegant and resolves all warnings.

funnel20 avatar Sep 03 '24 11:09 funnel20

@evgenyneu I have prepared a pull request for you: https://github.com/evgenyneu/Cosmos/pull/205

funnel20 avatar Sep 03 '24 12:09 funnel20