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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
This is what you need if you want to work with Swift and Objective C - itunes.apple.com/WebObjects/MZStore.woa/wa/…
It says the item is not available in the US Store. Do you know the name of the book?
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 :(
I see. Thanks for your help again. I actually have another question but I'm not sure if I should make it separate question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.