Missing Swift-ObjC references and Swift class methods are not exported?problem

Asked 2 years ago, Updated 2 years ago, 189 views

We make the iOS plug-in for Unity based on Swift.
However, to use SQLite, we have loaded the FMDB from the source file (.m/.h).
Maybe that's why I'm looking at the page where I generate multiple UnityPlugins on Swift, but it's not going well.

The Wrapper class is defined so that the Swift class can be referenced from ObjC as follows, but the Example class method of Swift described below cannot be referenced from Obj-C, and the error No known class method for selector 'swiftMethod' appears.

[Question 1] I think it's because Swift.h doesn't have a description of the model (Extern) of the method I want to refer to from the Wrapper class, so I can't see any of the methods from Objective-C. If anyone knows why Swift.h doesn't describe the model (Extern) of the method.

Example.mm

#import<UIKit/UIKit.h>
///#import<Foundation/Foundation.h>
#import<MyUnityPlugin-Swift.h>
//// This header file is generated automatically when Xcode build runs.
//

# ifdef__cplusplusplus
US>extern "C" {
#endif
    void_ex_callSwiftMethod(const char*message){
        // You can access Swift classes directly here.
        Example swiftMethod; // No known class method for selector 'swiftMethod'
        [Example swiftMethod]Method]; // No known class method for selector 'swiftMethod'
    }
# ifdef__cplusplusplus
}
#endif

Example.swift

import Foundation

public classExample:NSObject {
    public static func swiftMethod() {
        print("\"(#function)message:")
    }

    public functest(){
        print("test1 on ObjC");
    }

    public static func test2(test:NSString){
        print("test2")
    }
}

public func Test2(){
    print("test2");
}

auto-generated Swift.h

SWIFT_CLASS("_TtC13MyUnityPlugin7Example")
@ interfaceExample:NSObject
- (nonnull instance type) init OBJC_DESIGNATED_INITIALIZER;
@end

Using Bridging-Header.h and <ProjectName>-Swift.h (hereinafter abbreviated as Swift.h) seems to be causing circular references. The problem begins with:

Cannot find protocol declaration for 'CLLocationManagerDelegate'; did you mean 'NSLayoutManagerDelegate'?`

After checking the error, I assume that the header/import (including ?) is a circular reference (corrected to a reference defect).
I guess the circular reference (correction to lack of reference) is due to Bridging-Header.h and Swift.h.

[Question 2] In Swift.h, the CLLocationManagerDelegate that caused the above is written. Could you tell me how not to write it in Swift.h as a solution? (*This is a manual edit of Swift.h, but it's also a burden to edit it every time.)Bridging-Header.h

//For FMDB
# import "FMDatabase.h"
# import "FMResultSet.h"
# import "FMDatabaseAdditions.h"
# import "FMDatabaseQueue.h"
# import "FMDatabasePool.h"

<ProjectName>-Swift.h

//Generated by Apple Swift version 4.1 (swiftlang-902.0.48clang-902.0.37.1)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wgcc-compat"

[Omitted]

# if __has_include (<swift/objc-prologue.h>)
# include<swift/objc-prologue.h>
#endif

# pragma clang diagnostic ignored "-Wauto-import"
# include <objc/NSObject.h>
# include <stdint.h>
# include <stddef.h>
# include<stdbool.h>

[Omitted]

# if__has_feature(modules)
@importUIKit;
@importObjectiveC;
@import CoreLocation;
#endif

[Omitted]

@class UIWindow;
@class UIAplication;

SWIFT_CLASS("_TtC13 MyUnityPlugin11 AppDelegate")
@interfaceAppDelegate:UIResponder<UIApplicationDelegate>
@property(nonatomic, strong)UIWindow*_Nullable window;
- (BOOL) application: (UIApplication*_Nonnull) application didFinishLaunchingWithOptions: (NSDictionary<UIApplicationLaunchOptionsKey, id>*_Nullable) launchOptions SWIFT_WARN_UNUSED_RESULT;
- (void) applicationWillResignActive: (UIAapplication*_Nonnull) application;
- (void) applicationDidEnterBackground: (UIAapplication*_Nonnull) application;
- (void) applicationWillEnterForeground: (UIAapplication*_Nonnull) application;
- (void) applicationDidBecomeActive: (UIAapplication*_Nonnull) application;
- (void) applicationWillTerminate: (UIAapplication*_Nonnull) application;
- (nonnull instance type) init OBJC_DESIGNATED_INITIALIZER;
@end


SWIFT_CLASS("_TtC13MyUnityPlugin7Example")
@ interfaceExample:NSObject
- (nonnull instance type) init OBJC_DESIGNATED_INITIALIZER;
@end


[Omitted]

@class CLLocationManager;
@classCLLocation;

@ interface MyLocationService(SWIFT_EXTENSION(MyUnityPlugin))<CLLocationManagerDelegate>
- (void) locationManager: (CLLocationManager*_Nonnull) manager didChangeAuthorizationStatus: (CLAauthorizationStatus) status;
- (void) locationManager: (CLLocationManager*_Nonnull) manager didUpdateLocations: (NSArray<CLLocation*>*_Nonnull) locations;
@end

[Omitted]

swift ios xcode objective-c unity3d

2022-09-30 21:32

2 Answers

About Question 1

Swift4 does not generate an entry point for ObjC unless very limited conditions are met.Try adding @objc before the required method.

About [Question 2]

Rather than circular reference, I think it's just that I can't find the definition of CLLocationManagerDelegate as the message says.Wouldn't it be more certain to import CoreLocation before importing -Swift.h, like Example.mm's import of UIKit?Even if you don't use it from ObjC, I think it's better than suspicious to modify -Swift.h every time.


2022-09-30 21:32

Dear OOper,

[Answer to Question 1] I got the following from the implementation you gave me. I can see the entry point SwiftMethod, so I think I can probably refer to it from ObjC. Thank you.

SWIFT_CLASS("_TtC13MyUnityPlugin7Example")
@ interfaceExample:NSObject
+ (void) swiftMethod;
- (nonnull instance type) init OBJC_DESIGNATED_INITIALIZER;
@end

】Answer to Question 2 >

The CLLocationManagerDelegate class is used around the Swift class (internal), but will not be available for external (Unity).

CLLocationManagerDelegate Inheritance/Related Swift class
despite not using ObjC The generated Swift.h contains entry points for those Swift classes, which seems to result in reference-related errors.

If there is a good way to avoid generating entry points to Swift.h.
(I tried to understand the intent, but in order to do as you pointed out, I wish I had a .mm file of the ObjC class referring to the CLLocationManagerDelegate class, but I don't have one.)


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.