How to extend String?

(wdb) #1

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)]
                    
0 Likes

(marc hoffman) #2

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

0 Likes

(wdb) #3

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)]

0 Likes

(marc hoffman) #4

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.

0 Likes

(wdb) #5

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.

0 Likes

(marc hoffman) #6

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

1 Like

(wdb) #7

Cool :smiley:

0 Likes