Skip to content

Instantly share code, notes, and snippets.

View deathlezz's full-sized avatar
📱
Learning Swift 

DEATHLEZZ deathlezz

📱
Learning Swift 
View GitHub Profile
@deathlezz
deathlezz / Sets.swift
Created October 17, 2021 08:58
Simple use of sets in Swift 5.
//
// 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"]
@deathlezz
deathlezz / Arrays.swift
Created October 17, 2021 08:35
Simple use of arrays in Swift 5.
//
// Simple use of arrays
//
// basic array
var band = ["Steve", "Bob", "Kevin"]
print(band[0]) // Steve
band.append("Jack")
print(band) // ["Steve", "Bob", "Kevin", "Jack"]
@deathlezz
deathlezz / Loops.swift
Created October 17, 2021 08:12
Simple use of loops in Swift 5.
//
// Simple use of loops
//
// for loop
let range = 1...5
for i in range {
print("Number is \(i)")
}
// Number is 1
@deathlezz
deathlezz / Dictionaries.swift
Created October 17, 2021 07:30
Simple use of dictionaries in Swift 5.
//
// Simple use of dictionaries
//
// basic dictionary
var age = [
"Jack": 23,
"Alice": 25
]
age["Mark"] = 45
@deathlezz
deathlezz / Functions.swift
Created October 16, 2021 10:19
Simple use of functions in Swift 5.
//
// Simple use of functions
//
// basic function
func description() {
print("Here's Johnny!")
}
description() // Here's Johnny!
@deathlezz
deathlezz / Ternary operator.swift
Created October 16, 2021 09:26
Ternary operator in Swift 5.
//
// Ternary operator
//
// example #1
var first = 4
var second = 5
print(first == second ? "Numbers are equal" : "Numbers are different") // Numbers are different
@deathlezz
deathlezz / Optionals.swift
Last active October 16, 2021 08:40
Simple use of optionals in Swift 5.
//
// Simple use of optionals
//
// unwrapping with if let
var nickname: String? = nil
if let unwrap = nickname {
print("Successfully created user as: \(unwrap).")
} else {
@deathlezz
deathlezz / Protocols.swift
Last active October 16, 2021 09:07
Simple use of protocols in Swift 5.
//
// Simple use of protocols
//
// basic protocol
protocol Info {
var name: String { get set }
}
struct User: Info {
@deathlezz
deathlezz / Closures.swift
Last active October 11, 2021 16:08
Simple use of closures in Swift 5.
//
// Simple use of closures
//
// basic closure
let basic = {
print("I like pizza.")
}
basic() // I like pizza.
@deathlezz
deathlezz / Timer.swift
Last active November 1, 2021 21:23
Timer App in Swift 5.
//
// 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