This method requires padding with "=", the string length must be a multiple of 4.
In some base64 implementations, a decoding character is not required for decoding, since the number of bytes skipped can be calculated. But in the implementation of the Fund it is necessary.
Updated: As noted in the comments, it is recommended that you first check if the string length is already a multiple of 4. If encoded64 has your base64 string and is not constant, you can do something like this:
Swift 2
let remainder = encoded64.characters.count % 4 if remainder > 0 { encoded64 = encoded64.stringByPaddingToLength(encoded64.characters.count + 4 - remainder, withPad: "=", startingAt: 0) }
Swift 3
let remainder = encoded64.characters.count % 4 if remainder > 0 { encoded64 = encoded64.padding(toLength: encoded64.characters.count + 4 - remainder, withPad: "=", startingAt: 0) }
Updated single line version:
Or you can use this version of one line, which returns the same line when its length is already a multiple of 4:
encoded64.padding(toLength: ((encoded64.characters.count+3)/4)*4, withPad: "=", startingAt: 0)
source share