James Lei James Lei - 1 year ago 237
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)

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?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download