Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jamesrochabrun/4f23ea83b844b30d00f54686c81e1753 to your computer and use it in GitHub Desktop.
Save jamesrochabrun/4f23ea83b844b30d00f54686c81e1753 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
//Arrays
var teams: Array = ["barce", "rm", "atle"]
var champ: Array = ["champ", "liga", "eiro"]
var position: Array = [1,2,3,4]
var winer: [Bool] = [true, false, true]
var luckyNumbers: [Int] = [1,3,3]
var weights: [Double] = [2.0, 3.9]
//initializing an array
var subjects: [String] = []
//add elements
subjects.append("swift")
subjects.count
//more
teams.first
teams.last
teams[1]
position[position.count - 1]
position[3] = 5
position
//adding and deleting elements in array
position.insert(4, at: 4)
position.remove(at: 3)
position
//position.removeAll()
position.count
//contains ?
champ.contains("liga")
position.contains(2)
//find index from element
champ.index(of: "liga")
if champ.contains("liga") {
champ.remove(at: champ.index(of: "liga")!)
}
champ
//NEW IN SWIFT 3 arrays
champ += ["worldcup", "new"]
champ
//recover objects in a subarray form positions
champ[0...2]
//DICTIONARIES
var dict: [String: String] = ["primero":"numero 1",
"segundo" :"numero 2",
"tercero" :"numero 3",
"cuarto" : "numero 4"]
dict["primero"]
//añadiedno keys and values
dict["quinto"] = "numero 5"
dict.count
dict.updateValue("numero 7", forKey: "primero")
dict
//initializing dictionaries
var myDict : [Int : Float] = [:]
myDict[3] = 2.5
if myDict.isEmpty {
print("dict is empty")
}
//checking for unexisting keys, optional binding
//this number is not optional -- number
if let number = dict["quinto"] {
print(number)
} else {
print("no number founded")
}
//optional binding
var airports = [String :String]()
airports = ["Pmi" : "lima" , "auc" : "auckland", "tms": "tazmania"]
if let airport = airports["auc"] {
print("\("the airport") \(airport) \("exists")")
}
if let oldAirport = airports.removeValue(forKey: "tms") {
print(oldAirport)
}
airports
//OPTIONALS
//here is fine because the string was initialized
var greeting: String
greeting = "hola"
print(greeting)
//greeting = nil
var people: String?
people = "soy un string optional"
print(people)
people = nil
print(people)
if let people = people {
print(people)
}
//requeridos: constantes por ejemplo
var requiredString: String!
requiredString = "soy requerida"
requiredString = nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment