首页 > 解决方案 > SDK:iOS静态库实现

问题描述

我目前正在开发将所有网络与我的后端封装在一起的 SDK。我尝试根据 UML 创建一个类:在此处输入图像描述

但在共享实例方面遇到了困难。我想这是一个单例实例,我做了一个像:

public class MIDNetwork {

/// Class sigleton instance
static public let sharedManager:MIDNetwork = MIDNetwork()

但我不明白为什么我不能通过共享实例调用类的方法。我在测试项目中导入我的静态库并尝试调用这样的方法:

func handleRequest() {
    self.request = MIDNetwork.sharedManager.
    
}

这是我的课:

    public class MIDNetwork {
    
    /// Class sigleton instance
    public static let sharedManager:MIDNetwork = MIDNetwork()
    
    /// A network session
    private var networkSession:URLSession = URLSession(configuration: .default)
    
    ///baseUrl is represented as an API endpoint
    private static var baseUrl:URL = URL(string: "")!
    
    // TODO: - Somehow figure out a client url
    private var clientUrl:URL {
        return self.clientUrl
    }
    
    func createRootIdentity(requestDataMode:MIDCreateRootIdentityRequest, handler: @escaping (APIResult<MIDIdentity>) -> Void) -> URLSessionTask? { }

    func createDerivedIdentity(requestDataMode:MIDCreateDerivedIdentityRequest, handler: @escaping (APIResult<MIDDerivedIdentity>) -> Void) -> URLSessionTask? { }
    
    func faceCheck(requestDataMode:MIDFaceCheckRequest, handler: @escaping (APIResult<MIDFaceCheckResult>) -> Void) -> URLSessionTask? { }
    
    func getIdentityAdditionalInfo(derivedIdentityID:String, handler: @escaping (APIResult<MIDDerivedIdentityAdditionalInfo>) -> Void) -> URLSessionTask? { }
}

我尝试用 public 标记方法,但之后出现错误:

方法不能声明为 public,因为它的参数使用了内部类型

谁能解释我在哪里犯了错误?感谢您的回答,谢谢!

标签: iosswiftclasssdkencapsulation

解决方案


You have to declare your public methods, if you get the error

Method cannot be declared public because its parameter uses an internal type

it means that one of your parameters is not public

For example for the function:

func createRootIdentity(requestDataMode:MIDCreateRootIdentityRequest, handler: @escaping (APIResult<MIDIdentity>) -> Void) -> URLSessionTask? { }

Are MIDCreateRootIdentityRequest, APIResult, MIDIdentity declared publics?

The function is dependent on the parameters so either everything is declared public or you cannot use it


推荐阅读