Last active
February 6, 2016 05:56
-
-
Save leopic/7a6f159328dba97c2289 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (var|let) nombre:[tipo] = valor | |
// Variable: su valor cambiará dentro del programa | |
var cadenaDeTexto = "Hola Mundo!" | |
// Constante: su valor no cambiará | |
let numero = 4 | |
// Arreglo de Strings | |
let arregloDeTexto = ["uno", "dos", "tres", "catorce"] | |
// El compilador infiere el tipo de la variable | |
var cadenaDeTexto = "Hola Mundo!" | |
// Es igual a | |
var cadenaDeTexto:String = "Hola Mundo!" | |
let numero = 4 | |
// Es igual a | |
let numero:Int = 4 | |
// Válido | |
let constante = "Constante" | |
// Una constante no puede cambiar valor | |
constante = "nuevo valor" | |
// Válido | |
var numero = 5 | |
// Conversión inválida tipo | |
numero = "Cinco" | |
let nombre: String | |
// No usamos ()'s | |
if algunaCondicion { | |
nombre = "Ash" | |
} else { | |
nombre = "Orta" | |
} | |
print(nombre) | |
for var i = 0; i < 10; i++ { | |
print("Hola: \(i)") | |
} | |
for i in 0..<10 { | |
print("Hi: \(i)") | |
} | |
func translate(english: String) -> String { | |
return "something" | |
} | |
func translate(english: String) -> String { | |
switch english.lowercaseString { | |
case "hi": | |
return "Bonjour" | |
case "what time is it?": | |
return "Quelle heur fait-il?" | |
default: | |
return "" | |
} | |
} | |
func translate(english: String) -> String? { | |
switch english.lowercaseString { | |
case "hi": | |
return "Bonjour" | |
case "what time is it?": | |
return "Quelle heur fait-il?" | |
default: | |
return nil | |
} | |
} | |
let translatedString = translate("Hi") | |
if let translatedString = translate("Hi") { | |
print("Translated as '\(translatedString)'") | |
} else { | |
print("Translation failed, contact support.") | |
} | |
let stringTraducido = traducir("hola") | |
print("Resultado \(stringTraducido)") | |
// "Resultado Optional("Hello")\n" | |
// Creamos un arreglo vacio | |
var arreglo = [String]() | |
// Agregamos un elemento | |
arreglo.append("hola") | |
// El primer elemento es un Opcional | |
if let primeraEntradaPre = arreglo.first { | |
print("Iniciamos con: \(primeraEntradaPre)") | |
} else { | |
// En caso de no existir | |
print("Arreglo vacio") | |
} | |
// Acceder directamente un elemento | |
print(arreglo[0]) | |
// Iterando sobre los elementos | |
for entrada in arreglo { | |
print(entrada) | |
} | |
// Revisamos si tiene contenido | |
if !arreglo.isEmpty { | |
// Total entradas | |
print("Count: \(arreglo.count)") | |
// Removemos el primer elemento | |
arreglo.removeAtIndex(0) | |
} | |
if let primeraEntradaPost = arreglo.first { | |
print("Iniciamos con: \(primeraEntradaPost)") | |
} else { | |
print("Arreglo vacio") | |
} | |
// Creamos un diccionario vacio, contiene: | |
// Strings como llaves | |
// Valores son Ints | |
var diccionario = [String:Int]() | |
// Agregamos un elemento | |
diccionario["hola"] = 1 | |
// El primer elemento es Opcional | |
if let primeraEntradaPre = diccionario.first | |
{ | |
print("Llave: \(primeraEntradaPre.0). Valor \(primeraEntradaPre.1)") | |
} else { | |
// En caso de no existir | |
print("Diccionario vacio") | |
} | |
// Acceder directamente un elemento | |
print(diccionario["hola"]) | |
// Iterando sobre los elementos | |
for entrada in diccionario { | |
print(entrada) | |
} | |
// Iterando sobre los elementos, separados como llave, valor | |
for (key, value) in diccionario { | |
print("\(key) : \(value)") | |
} | |
if !diccionario.isEmpty { | |
// Total entradas | |
print("Count: \(diccionario.count)") | |
// Removemos el único elemento | |
diccionario.removeValueForKey("hola") | |
} | |
if let primeraEntradaPost = diccionario.first | |
{ | |
print("Llave: \(primeraEntradaPost.0). Valor \(primeraEntradaPost.1)") | |
} else { | |
print("Diccionario vacio") | |
} | |
// Función con más de un parámetro | |
// Dividido por comas | |
func saludo(nombre: String, dia: String) -> String { | |
return "Hola \(nombre), hoy es \(dia)." | |
} | |
// Invocado con el nombre del parámetro | |
saludo("Esteban", dia: "Lunes") | |
// Función con valores predeterminados | |
func segundoSaludo(nombre:String = "Chris", dia:String = "Martes") -> String { | |
// Retornamos el llamado a la función anterior | |
return saludo(nombre, dia: dia) | |
} | |
// Sin parámetros | |
segundoSaludo() // Hola Chris, hoy es Martes. | |
// Con solo un parámetro | |
segundoSaludo("Jose") // Hola Jose, hoy es Martes. | |
// Con ambos parámetros | |
segundoSaludo("Leo", dia: "Miércoles") // Hola Leo, hoy es Miércoles. | |
// Operaciones con Strings | |
// Interpolaciones | |
let humano = "leo" | |
var salida = "Hola \(humano)." | |
// Reemplazos | |
let base = "Hola Humano" | |
let remplazo = base.stringByReplacingOccurrencesOfString("Hola", withString: "Adios") | |
// Verificar contenido | |
let stringABuscar = "Chris Esteban Leo" | |
stringABuscar.rangeOfString("Leo") | |
// Convertir a array usando " " como separador | |
let profesores = "Chris Esteban Leo" | |
profesores.componentsSeparatedByString(" ") | |
// Verificar contenido | |
let nombre = "leo" | |
"leo".isEmpty | |
// Remover caracteres en blanco | |
let nombreConEspacios = " leo " | |
nombreConEspacios.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment