Skip to content

Instantly share code, notes, and snippets.

@novellizator
Created October 29, 2017 14:52
Show Gist options
  • Save novellizator/16990b3b64e9e901946918affc4b8729 to your computer and use it in GitHub Desktop.
Save novellizator/16990b3b64e9e901946918affc4b8729 to your computer and use it in GitHub Desktop.
Demos from the Swift Workshop (Novella & Dedecek)
//Swift Workshop
import Foundation
// Declaring variables
let number = 0
let first = "first"
let second: String = "second"
var counter = 0
counter += 1
// Control flow
if counter > 0 {
print("Count is greater then 0")
}
guard counter > 0 else {
print("Count is less then or equal")
fatalError()
}
let type = 2
switch type {
case 0:
"zero"
break
case 1:
"one"
break
case let t where t >= 2 && t < 5:
t
break
default:
break
}
let isZero = counter == 0
switch isZero {
case true:
"true"
break
case false:
"false"
break
}
// Iterating
// for (;;) {}
for i in 0..<5 {
print(i)
}
for i in stride(from: 0, to: 10, by: -1) {
print(i)
}
while (false) {
}
repeat {
} while (false)
// Tuples
let pair = (0, 0)
let namedPair = (x: 0, y: 0)
//let x = namedPair.x
//let y = namedPair.1
let (x, y) = namedPair
// Strings
let fullName = "Josef Novak"
fullName.prefix(5)
fullName.suffix(6)
let indexOfSpace = fullName.index(of: " ")!
fullName[..<indexOfSpace]
let indexAfterSpace = fullName.index(after: indexOfSpace)
fullName[indexAfterSpace...]
//let z = cd[cd.startIndex..<cd.endIndex]
// Optionals
var mayBeString: String?
mayBeString = "Hello"
if let string = mayBeString {
string
}
let value = Int("12")
// Arrays
let array = ["a", "b", "c", "d"]
for element in array {
print(element)
}
for (index, element) in array.enumerated() {
print(index, element)
}
let subarray = Array(array[..<2])
let numbers = ["a", "", "c"]
let index = numbers.index { (number) in
number.isEmpty
}
index != nil
if let index = index {
numbers[index]
}
// Dictionaries
var nameToAgeMap = [
"Josef": 12,
"Eva": 9
]
nameToAgeMap["Josef"]
nameToAgeMap["Lenka"]
nameToAgeMap["Lenka"] = 14
if let lenkaAge = nameToAgeMap["Lenka"] {
lenkaAge
nameToAgeMap.removeValue(forKey: "Lenka")
}
for (name, age) in nameToAgeMap {
print(name, age)
}
// Functions
func avg(number1: Int, number2: Int) -> Int {
return (number1 + number2) / 2
}
//avg(number1: 10, number2: 20)
//avg(10, 20)
func sum(numbers: [Int]) -> Int {
var result = 0
for number in numbers {
result += number
}
return result
}
sum(numbers: [1, 1, 1])
let printHello = { () -> Void in
"Hello"
}
printHello()
func invoke(_ function: () -> Void) {
function()
}
invoke(printHello)
// Struct & Class
struct Complex {
var real: Double
var imag: Double
}
let c1 = Complex(real: 5, imag: 0)
//c1.real = 10
var c2 = c1
c2.real = 10
c1.real
let a1 = [0]
var a2 = a1
a2.append(2)
a1
class Person {
var name: String = ""
}
let p = Person()
p.name = "Josef"
let p1 = p
p1.name = "Jan"
p.name
// Constructors & destructors
class Watcher {
var name: String = ""
init() {
print("Object created")
}
deinit {
print("Object released")
}
}
var w: Watcher?
w = Watcher()
// Do something
w = nil
//
//Reference Counting Demo
//
import Foundation
// Retain count of object
var object = NSObject()
CFGetRetainCount(object)
let nextObject = object
CFGetRetainCount(object)
func retainCount(of object: NSObject) {
CFGetRetainCount(object)
}
retainCount(of: object)
// Weak references
weak var weakObject = object
CFGetRetainCount(object)
do {
weakObject = NSObject()
}
weakObject
// Retain count of object in Array
let array = [NSObject()]
CFGetRetainCount(array[0])
var newArray = array
CFGetRetainCount(array[0])
newArray.append(NSObject())
CFGetRetainCount(array[0])
func retainCount(of objects: [NSObject]) {
CFGetRetainCount(objects[0])
}
retainCount(of: array)
// Custom copy-on-write type
class _MagixBox<T> {
var value: T
init(value: T) {
self.value = value
}
}
struct MagicBox<T> {
var box: _MagixBox<T>
init(value: T) {
self.box = _MagixBox(value: value)
}
var value: T {
get {
return box.value
}
set {
if isKnownUniquelyReferenced(&box) {
box.value = newValue
} else {
box = _MagixBox(value: newValue)
}
}
}
}
var b = MagicBox(value: 0)
b.value
var b2 = b
b.value
b2.value = 10
b.value = 1
b2.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment