0

I am trying to make use of an Objective-C API in Swift. I can only seem to call the shareMyInstance() method from Swift, and not the initOpenApi() method for some reason. I'm not sure if there is some sort of scope identifier present in the interface, but I can't make use of initOpenApi, even though both are in the header. I also cannot see the method bodies, but I don't believe that affects the scope of the function.

This is locking me into using Objective-C, because for some reason I can access all of the functions from Objective-C, but only 3 of the 4 from Swift.

Header file (LCOpenSDK_Api.h):

#ifndef LCOpenSDK_LCOpenSDK_Api_h
#define LCOpenSDK_LCOpenSDK_Api_h
#import <Foundation/Foundation.h>
@interface LCOpenSDK_Api: NSObject
+ (LCOpenSDK_Api*) shareMyInstance;
- (id) initOpenApi:(NSString*)addr port:(NSInteger)port CA_PATH:(NSString*)caPath;
- (NSInteger)request:(void*)req resp:(void*)resp timeout:(NSInteger)timeout;
- (void)uninitOpenApi;
@end
#endif

My code (.swift):

import Foundation

@objc(LeChangePlayerView)
class LeChangePlayerView: UIView {
  
  //...
  
  @objc
  override init(frame: CGRect) {
    super.init(frame: frame)
    var lc = LCOpenSDK_Api.shareMyInstance()!; //Fine

    //Need this function!
    lc.initOpenApi("openapi.easy4ip.com", 443, "") //Value of type 'LCOpenSDK_Api' has no member 'initOpenApi'
  }

The only possible other explanation is that there is a different header file with the same name, but different interface, but this is highly unlikely because shareMyInstance, request and unitOpenApi are all available, and going to the definition from within the swift file using Xcode points to the same file. It is a dynamic framework, and as of right now, I can only view the headers, not the method implementations. I'm not sure if there's a solution to this, but this is another problem I could use help with. Could they have locked the original source code somehow, as well as made that specific method private?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jonathan Ma
  • 353
  • 3
  • 11
  • this should help: https://stackoverflow.com/questions/56963724/form-c-header-file-vector-file-not-found-error-happening-into-swift basically they say that this library is C++, not C, so integration from Swift is different – timbre timbre Jun 22 '20 at 21:52

1 Answers1

1

Although initOpenApi is an instance method, Swift recognises it as an initialiser as it starts with the word init. Initialisers are translated from Objective-C into Swift-style initialisers.

In Objective-C you would say something like [[LCOpenSDK_Api alloc] initOpenAPI:@"openapi.easy4ip.com", port: 443, CA_PATH: @""]

In Swift the word init is stripped and there is no need to explicitly allocate a new instance:

let lc = LC_OpenSDK_Api(openApi:"openapi.easy4ip.com:, port: 443, CA_PATH:"")

However, you need to refer to the documentation from the framework to determine if you want to access the singleton instance LC_OpenSDK_Api.shareMyInstance or whether you want to create a specific instance as I showed above.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Oh my god! This worked for me! I have been stuck on this problem for 3 weeks and was trying to do the whole thing a roundabout way by reusing components from demo objective c source files, instead of directly using the component classes. What a headache, I wish this were documented somewhere in the tens of bridge tutorials I looked through. Thanks for answering my question! Is there another place where these initializer maps are described? I have several Objective C classes to make use of. – Jonathan Ma Jun 23 '20 at 03:56
  • This is what you need if you want to work with Swift and Objective C - https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewBook?id=888894773 – Paulw11 Jun 23 '20 at 04:03
  • It says the item is not available in the US Store. Do you know the name of the book? – Jonathan Ma Jun 23 '20 at 20:39
  • It's called "Using Swift with Cocoa and Objective-C" but it may have been removed. I found a developer forum post saying it wasn't being updated because the content was in the swift interoperability documentation, but I just took a look at that documentation and it is much less helpful than the book :( – Paulw11 Jun 23 '20 at 21:13
  • I see. Thanks for your help again. I actually have another question but I'm not sure if I should make it separate question. – Jonathan Ma Jun 23 '20 at 21:16
  • Yes, make it a separate question. – Paulw11 Jun 23 '20 at 21:16