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
// | |
// Simple use of sets | |
// | |
// basic set | |
var colors = Set(["yellow", "green", "green", "black", "purple"]) | |
print(colors) // ["purple", "yellow", "black", "green"] | |
colors.remove("green") | |
print(colors) // ["black", "purple", "yellow"] |
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
// | |
// Simple use of arrays | |
// | |
// basic array | |
var band = ["Steve", "Bob", "Kevin"] | |
print(band[0]) // Steve | |
band.append("Jack") | |
print(band) // ["Steve", "Bob", "Kevin", "Jack"] |
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
// | |
// Simple use of loops | |
// | |
// for loop | |
let range = 1...5 | |
for i in range { | |
print("Number is \(i)") | |
} | |
// Number is 1 |
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
// | |
// Simple use of dictionaries | |
// | |
// basic dictionary | |
var age = [ | |
"Jack": 23, | |
"Alice": 25 | |
] | |
age["Mark"] = 45 |
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
// | |
// Simple use of functions | |
// | |
// basic function | |
func description() { | |
print("Here's Johnny!") | |
} | |
description() // Here's Johnny! |
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
// | |
// Ternary operator | |
// | |
// example #1 | |
var first = 4 | |
var second = 5 | |
print(first == second ? "Numbers are equal" : "Numbers are different") // Numbers are different | |
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
// | |
// Simple use of optionals | |
// | |
// unwrapping with if let | |
var nickname: String? = nil | |
if let unwrap = nickname { | |
print("Successfully created user as: \(unwrap).") | |
} else { |
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
// | |
// Simple use of protocols | |
// | |
// basic protocol | |
protocol Info { | |
var name: String { get set } | |
} | |
struct User: Info { |
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
// | |
// Simple use of closures | |
// | |
// basic closure | |
let basic = { | |
print("I like pizza.") | |
} | |
basic() // I like pizza. |
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
// | |
// Timer App | |
// | |
import Foundation | |
// time left to finish | |
var (hours, minutes, seconds) = (1, 0, 3) | |
// convert hours, minutes and seconds to seconds and add them together |
NewerOlder