James Lei James Lei - 1 month ago 42
Swift Question

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

Answer

Use this instead:

let longstring = "test123"
let data = (longstring).data(using: String.Encoding.utf8)
let base64 = data!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))

print(base64)// dGVzdDEyMw==\n