How to extend String?

I would like to extend String so it has a helper method for encoding a base64 and my first attempt concentrating on Cocoa was:

public extension String {
    
    public func fromBase64() -> String? {
        return self
    }
    
    public func toBase64() -> String {
       return self
    }
}

but when I try in my code like:

 No member "toBase64" on type "String!" [/Users/x/Development/Projects/x/x/version-3/config-app/SharedProject/AuthService.swift (31)]
                    

That should work, and its what SBL does, itself. Some context might help determine why it fails for you…

Yeah, so when I do the following the same project (a SharedProject):

        let credentials: String = "\(username):\(password)"
        var encodedCredentials = credentials.toBase64()

I am getting:
No member "toBase64" on type "String!" [/Users/x/Development/Projects/x/x/version-3/config-app/SharedProject/AuthService.swift (39)]

Can you show me a compete project? there’s a lot of variables, w/o a concrete test case I’ll be chasing my own tail trying tom reproduce the issue – I don’t even know what platform.

In the latest develop build 2395 it appears to be building successfully. If you still want to have the non-working code base. I will be happy to share it with you!

Now I only need to find a way to implement the encoding to base64 for strings. I think I need to find a Objective-C NSString code snippet for this.

isn’t there something in elements rtl already that does base64?

1 Like

Cool :smiley: