let string = "Hello World"
let utf8EncodeData = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
// 将NSData进行Base64编码
let base64String = utf8EncodeData?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: UInt(0)))
print("encodedString: \(base64String!)")
// 结果
encodedString: SGVsbG8gV29ybGQ=
// 将base64字符串转换成NSData
let base64Data = NSData(base64Encoded:base64String!, options:NSData.Base64DecodingOptions(rawValue: 0))
// 对NSData数据进行UTF8解码
let stringWithDecode = NSString(data:base64Data as! Data, encoding:String.Encoding.utf8.rawValue)
print("base64String \(stringWithDecode!)")
//结果
base64String Hello World