🐦swift swift grimoire

swift

fukurou

the supreme coder
ADMIN
randomizer

Int.random(in: 0...5) // includes 0 and 5
Int.random(in: 0..<5) // includes 0

array1.randomElement()! // returns a random element from the array
 

fukurou

the supreme coder
ADMIN
switch with range

Swift:
switch x {
case 80...90:
    print("x in range")

case x..<10:
    print("x is small")

case 50:
    print("50")

default:
    print("Have you done something new?")
}

:hmm: ...10 can replace x..<10
 

fukurou

the supreme coder
ADMIN
multi array

2d:
var cinema = [[Int]]()
or
var cinema = Array(repeating: Array(repeating: 0, count: 5), count: 5)

N-dimensional arrays


var cinemas = [[[Int]]]()
var cinemas = Array(repeating: Array(repeating: Array(repeating: 0, count: 5), count: 5), count: 3)
cinemaRooms[1][2][3] = 1 // the second-floor cinema, the third row, the fourth seat

iteration 3d array :

Swift:
var floor = 1

for cinema in cinemas {
    print("Floor: \(floor)")
    floor += 1
    for array in cinema {
        for value in array {
            print(value, terminator: " ")
        }
        print(" ")
    }
    print("-----------------")
}
 

fukurou

the supreme coder
ADMIN
struct

Swift:
// define a structure
struct Person {

// define two properties
 var name = ""
 var age = 0
}

// create instance of Person
var person1 = Person()

// access properties and assign new values
person1.age = 21
person1.name = "Rick"

print("Name: \(person1.name) and Age: \( person1.age) ")
 

fukurou

the supreme coder
ADMIN

Struct vs. Class in Swift


struct is pretty much a constant you can modify (with fields)
1 it gets passes as a value not as a reference
2 init is optional for struct
3 struct does have inheritance

 
Last edited:

fukurou

the supreme coder
ADMIN
mutating struct properties :

Swift:
struct Person {

// define two properties
 var name = ""
 var age = 0
    mutating func birthday(){
        self.age += 1
    }
}

// create instance of Person
var person1 = Person()
person1.birthday()
print(person1.age)
 

fukurou

the supreme coder
ADMIN
classes

playground for multiple swift files :
file->new->project->macOS->command line tool

adding a file :
right click project folder->new file->swift file


new swift file.png
help->developer documentation

class example with clone method (called copy in the swift language) :

Swift:
class Enemy{
    var health : Int = 100
    var ATK : Int = 10
    func walk(){
        print("walked")
    }
    func copy()->Enemy{
        let clone = Enemy()
        clone.health = self.health
        clone.ATK = self.ATK
        return clone
    }
}
 

fukurou

the supreme coder
ADMIN
optionals

crate a new Xcode project->macOS tab-> command line
optional binding

Swift:
let myOptional:String? = "hadoken"
// transform optional to actual variable (optional binding)
if let safeOptional = myOptional {
    let text:String = safeOptional
    print(text)
}

nil coalescing operator :

Swift:
let myOptional:String? = nil
/*
 nil coalescing operator
 the passed value will be the optional value or a default if the optional
 is a nil
 */
let text:String = myOptional ?? "it was a nil"
print(text) // prints it was a nil

optional chaning (a nil coalescing for object attributes)

Swift:
struct MyOptionl{
    var property = 1234
    func method(){print("prints me")}
}

let myOptional: MyOptionl? = MyOptionl()
let text:Int = myOptional?.property ?? 25
print(text) // prints it was a nil

optional binding :
Swift:
struct MyOptionl{
    var property = 1234
    func method(){print("prints me")}
}
let myOptional: MyOptionl? = MyOptionl()
// optional binding :
myOptional?.method() // (nil will not run)
 

fukurou

the supreme coder
ADMIN
swift getters and setters

Swift:
class Weight {
    var kilograms: Float = 0.0
    var pounds: Float {
        get {
            return kilograms * 2.205
        }
        set(newWeight) {
            kilograms = newWeight / 2.205
        }
    }
}
 
Top