Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
649 views
in Technique[技术] by (71.8m points)

ios - Extending a Public class for partial public access in Swift

I am working on a Swift framework which has majority of Swift files and very few Objective C files. Since I don't want my Objective-C files to be available outside the SDK and not even under headers folder, I gave them project access and now use them internally with a module map.

Here is how my modulemap file looks like:

module MySDKPrivate {

    header "CardDetails.h"
    export *
}

Now, in order to access this CardDetails file, I just need to import MySDKPrivate module in the required Swift class. All good till here.

Now, let's say I have a public class, named Payment.swift. This class is responsible for executing SDK operations partially hidden to the user, hence it is under the project access, so it does not show up in the headers when SDK is distributed.

There are some methods such as authenticate and getSessionExpireDate which are accessed by a PaymentDoor.swift class, which is basically nothing but an extension to Payment class for partial read & write injection, helping to get the minor info in/out of Payment class. Here is the sample signature of PaymentDoor.swift

//Extension to inject data in and out of the Internal Payment.swift
public extension Payment{

}

And here is the Payment.Swift class

import MySDKPrivate

public class Payment {
    let username: String
    let password: String
    let cardDetails: CardDetails

    public init(userName: String, password: String, cardNumber: String) {
        self.username = userName
        self.password = password

        let cardDetails = CardDetails()
        cardDetails.cardNumber = cardNumber

        self.cardDetails = cardDetails
    }

    public func authenticate() -> Bool{
        //Some logic
        return true
    }

    private func generateAuthkey(){
        //Should not be accessible with the extension
    }
}

This is another reason why I want to keep this Payment class as public and inside project access for headers, because If I make it available as public under headers, SDK user would see the name of my modulemap and all the code in Payment class and could use the objective-c files as well with importing the module map. Although now also the name of the module map comes up by Auto Help but at least it's not visible directly.

I still think there could be a better way to inject data in and out of a public class which hides my module name OR Hiding Objective-C files could be done in a better way may be. If yes, please help !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...