簡體   English   中英

不能在 Swift 中使用 Objective-C class 中的所有方法

[英]Cannot use all methods in Objective-C class in Swift

我正在嘗試在 Swift 中使用 Objective-C API。 由於某種原因,我似乎只能從 Swift 調用shareMyInstance()方法,而不是initOpenApi()方法。 我不確定界面中是否存在某種 scope 標識符,但我無法使用initOpenApi ,即使兩者都在 header 中。 我也看不到方法體,但我不認為這會影響 function 的 scope。

這使我無法使用 Objective-C,因為出於某種原因,我可以訪問 Objective-C 的所有功能,但只有 Swift 的 4 個功能中的 3 個。

Header 文件(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

我的代碼(.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'
  }

唯一可能的其他解釋是有一個不同的 header 文件具有相同的名稱,但不同的接口,但這不太可能,因為 shareMyInstance、request 和 unitOpenApi 都可用,並且使用 swift 文件中的定義到同一個文件。 它是一個動態框架,到目前為止,我只能查看標題,而不能查看方法實現。 我不確定是否有解決方案,但這是我可以使用幫助的另一個問題。 他們能否以某種方式鎖定原始源代碼,並將該特定方法設為私有?

盡管initOpenApi是一個實例方法,但 Swift 將其識別為初始化程序,因為它以單詞init開頭。 初始化器從 Objective-C 轉換為 Swift 風格的初始化器。

在 Objective-C 你會說類似[[LCOpenSDK_Api alloc] initOpenAPI:@"openapi.easy4ip.com", port: 443, CA_PATH: @""]

在 Swift 中,單詞init被剝離,無需顯式分配新實例:

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

但是,您需要參考框架中的文檔來確定是否要訪問 singleton 實例LC_OpenSDK_Api.shareMyInstance或者是否要創建一個特定的實例,如我上面所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM