Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created April 19, 2018 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitomad/8c9e28fbbb0267fa960feac4cbee1788 to your computer and use it in GitHub Desktop.
Save fitomad/8c9e28fbbb0267fa960feac4cbee1788 to your computer and use it in GitHub Desktop.
import Foundation
let texto: String = "Esto es una cadena de texto que va a pasar a Data y luego a String de nuevo"
print("1. \(texto)")
// Lo convertimos en `Data`
if let data = texto.data(using: .utf8)
{
print("2. Tamaño en bytes: \(data)")
// Y ahora creamos un `String` a partir de un `Data`
if let textoOtraVez = String(data: data, encoding: .utf8)
{
print("3. \(textoOtraVez)")
}
}
/*
1. Esto es una cadena de texto que va a pasar a Data y luego a String de nuevo
2. Tamaño en bytes: 75 bytes
3. Esto es una cadena de texto que va a pasar a Data y luego a String de nuevo
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment