Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to base64 in Swift 3

let strsize = 10_000_000
let tries = 100
var longstring:String = "a"

for i in 1...strsize {
longstring += "a"
}

for i in 1..<2 {
let basestring = NSData(base64EncodedString: longstring, options: .IgnoreUnknownCharacters)
print(basestring)
}

Writing a code in command prompt. What is the correct code to write for Swift 3 which I been getting use of unresolved identifier NSData . Most of the tutorials on encoding string to base64 aren't working.

This is working for you on Linux or Mac? http://studyswift.blogspot.sg/2016/03/convert-nsdatauint8-to-base64-in-swift.html

like image 220
James Lei Avatar asked Oct 25 '16 08:10

James Lei


1 Answers

Use this instead:

let longstring = "test123"
if let data = (longstring).data(using: String.Encoding.utf8) {
    let base64 = data.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0))
    print(base64)// dGVzdDEyMw==\n
}
like image 116
Rashwan L Avatar answered Sep 20 '22 23:09

Rashwan L