ios - decoding base 64 swift 2.3 -


i have following extension:

extension string {      func frombase64() -> string? {         guard let data = nsdata(base64encodedstring: self, options: nsdatabase64decodingoptions(rawvalue: 0)) else {             return nil         }          return string(data: data, encoding: nsutf8stringencoding)!     }      func tobase64() -> string? {         guard let data = self.datausingencoding(nsutf8stringencoding) else {             return nil         }          return data.base64encodedstringwithoptions(nsdatabase64encodingoptions(rawvalue: 0))     } } 

and have base 64 encoded string: r79gqdntt/0+cju7pduqfa==

func dostuff(sample : string){      let samplebase64decoded = sample.frombase64() } 

when calling function, (dostuff("r79gqdntt/0+cju7pduqfa==")) runtime error referring finding nil while unwrapping optional:

return string(data: data, encoding: nsutf8stringencoding)! 

what reasons there swift not being able convert base64 data string?


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -