// Plik 1:
let globalVariable = "global"
class Dog {
    func printGlobal() {
        print(globalVariable) // *
    }
}
// Plik 2:
class Cat {
    func printGlobal() {
        print(globalVariable) // *
    }
}



// Plik1:
class Dog {
    static let staticProperty = "staticProperty"
    let instanceProperty = "instanceProperty"
    func printInstanceProperty() {
        print(self.instanceProperty) // *
    }
}
// Plik2:
class Cat {
    func printDogStaticProperty() {
        print(Dog.staticProperty) // *
    }
    func printDogInstanceProperty() {
        let d = Dog()
        print(d.instanceProperty) // *
    }
}



class Dog {
    func printLocalVariable() {
        let localVariable = "local"
        print(localVariable) // *
    }
}



let timed : Bool
if val == 1 {
    timed = true
} else {
    timed = false
}



var r : CGFloat = 0
var g : CGFloat = 0
var b : CGFloat = 0
var a : CGFloat = 0
c.getRed(&r, green: &g, blue: &b, alpha: &a)



var bti : UIBackgroundTaskIdentifier
bti = UIApplication.shared.beginBackgroundTask {
    UIApplication.shared.endBackgroundTask(bti)
} // Błąd: zmienna przechwycona przez domknięcie przed jej inicjalizacją.



var bti : UIBackgroundTaskIdentifier = .invalid
bti = UIApplication.shared.beginBackgroundTask {
    UIApplication.shared.endBackgroundTask(bti)
}



let timed : Bool = {
    if val == 1 {
        return true
    } else {
        return false
    }
}()



class RootViewController : UITableViewController {
    let cellBackgroundImage : UIImage = {
        return imageOfSize(CGSize(width:320, height:44)) {
            // Miejsce na kod odpowiedzialny za narysowanie obrazu.
        }
    }()
    // Miejsce na pozostałą część klasy.
}



class RootViewController : UITableViewController {
    let cellBackgroundImage : UIImage = self.makeTheImage() // Błąd w trakcie kompilacji.
    func makeTheImage() -> UIImage {
        return imageOfSize(CGSize(width:320, height:44)) {
            // Miejsce na kod odpowiedzialny za narysowanie obrazu.
        }
    }
}



var now : String { // 1.
    get { // 2.
        return Date().description // 3.
    }
    set { // 4.
        print(newValue) // 5.
    }
}



var mp : MPMusicPlayerController {
    return MPMusicPlayerController.systemMusicPlayer
}
var nowPlayingItem : MPMediaItem? {
    return self.mp.nowPlayingItem
}



var authorOfItem : String? {
    guard let authorNodes =
        self.extensionElements(
            withXMLNamespace: "http://www.tidbits.com/dummy",
                elementName: "app_author_name")
        else {return nil}
    guard let authorNode = authorNodes.last as? FPExtensionNode
        else {return nil}
    return authorNode.stringValue
}



private var _myBigData : Data! = nil
var myBigData : Data! {
    set (newdata) {
        self._myBigData = newdata
    }
    get {
        if _myBigData == nil {
            // Pobranie odwołania do pliku, f, znajdującego się na dysku.
            if let d = try? Data(contentsOf:f) {
                self._myBigData = d
                // Usunięcie pliku.
            }
        }
        return self._myBigData
    }
}



var s = "cokolwiek" { // 1.
    willSet { // 2.
        print(newValue) // 3.
    }
    didSet { // 4.
        print(oldValue) // 5.
        // self.s = "coś innego".
    }
}



var angle : CGFloat = 0 {
    didSet {
        // Wartość kąta nie może być mniejsza niż 0 lub większa niż 5.
        self.angle = min(max(self.angle, 0), 5)
        // Modyfikacja interfejsu odzwierciedlająca zmianę.
        self.transform = CGAffineTransform(rotationAngle: self.angle)
    }
}



class MyView : UIView {
    lazy var arrow = self.arrowImage()
    func arrowImage () -> UIImage {
        // Miejsce na kod generujący duży obraz.
    }
}



lazy var prog : UIProgressView = {
    let p = UIProgressView(progressViewStyle: .default)
    p.alpha = 0.7
    p.trackTintColor = UIColor.clear
    p.progressTintColor = UIColor.black
    p.frame = CGRect(x:0, y:0, width:self.view.bounds.size.width, height:20)
    p.progress = 1.0
    return p
}()



func application(_ application: UIApplication,
    didFinishLaunchingWithOptions
    launchOptions: [UIApplication.LaunchOptionsKey : Any]?)
    -> Bool {



func application(_ application: UIApplication,
    didFinishLaunchingWithOptions
    launchOptions: [UIApplication.LaunchOptionsKey : Any]?)
    -> Bool {
        return true
}



let i = 10
let x = Double(i)
print(x) // Dane wyjściowe to 10.0, czyli wartość typu Double.
let y = 3.8
let j = Int(y)
print(j) // Dane wyjściowe to 3, czyli wartość typu Int.



func f() {
    let s = """
    Wiersz 1
        Wiersz 2
    Wiersz 3
    """
    // …
}



func f() {
    let s = """
    Wiersz "1"
        Wiersz 2 \
    to nadal jest wiersz 2
    """
    // …
}



let s = "Witaj,"
let s2 = "świecie!"
let space = " "
let greeting = [s,s2].joined(separator:space)



let s = "31"
let i = Int(s) // Wartość i wynosi Optional(31).
let s2 = "1f"
let i2 = Int(s2, radix:16) // Wartość i2 wynosi Optional(31).



let s = "witaj"
for c in s {
    print(c) // Wyświetlenie każdego obiektu Character w oddzielnym wierszu.
}



let s = "\u{BF}Qui\u{E9}n?"
for i in s.utf8 {
    print(i) // Dane wyjściowe to 194, 191, 81, 117, 105, 195, 169, 110, 63.
}
for i in s.utf16 {
    print(i) // Dane wyjściowe to 191, 81, 117, 105, 233, 110, 63.
}



func flag(country:String) -> String {
    let base : UInt32 = 127397
    var s = ""
    for v in country.unicodeScalars {
        s.unicodeScalars.append(UnicodeScalar(base + v.value)!)
    }
    return String(s)
}
// Oto sposób użycia zdefiniowanej funkcji.
let s = flag(country:"PL")



let s = "Ha\u{030A}kon"
print(s.count) // Dane wyjściowe: 5.
let length = (s as NSString).length // Ewentualnie można użyć s.utf16.count.
print(length) // Dane wyjściowe: 6.



let s = "witaj"
for c in s {
    print(c) // Wyświetlenie każdego obiektu Character w oddzielnym wierszu.
}



let s = "witaj"
let ix = s.startIndex
let ix2 = s.index(ix, offsetBy:1)
let c = s[ix2] // Wartością zwrotną jest "i".



let s = "witaj"
let arr = Array(s)
let result = arr[1...3]
let s2 = String(result) // Wartością zwrotną jest "ita".



let s = "witaj"
let ix1 = s.index(s.startIndex, offsetBy:1)
let ix2 = s.index(ix1, offsetBy:2)
let s2 = s[ix1...ix2] // Wartością zwrotną jest "ita".



var s = "witaj"
let ix = s.startIndex
let r = s.index(ix, offsetBy:2)...s.index(ix, offsetBy:4)
s.replaceSubrange(r, with: "atr") // Wartością s jest teraz "wiatr".



var s = "witaj"
let ix = s.startIndex
let r = s.index(ix, offsetBy:1)...s.index(ix, offsetBy:3)
s.removeSubrange(r) // Wartością s jest teraz "wj".



let s = "witaj"
for (ix,c) in s.enumerated() {
    print("znak \(ix) to \(c)")
}



var pair = (first:1, second:"Dwa")
let x = pair.first // Wartością x jest 1.
pair.first = 2
let y = pair.0 // Wartością y jest 2.



let s = "witaj"
for t in s.enumerated() {
    print("znak \(t.offset) to \(t.element)")
}



func tupleMaker() -> (first:Int, second:String) {
    return (1, "Dwa") // Brak etykiet.
}
let ix = tupleMaker().first // Wartością ix jest 1.



class Board {
    typealias Point = (x:Int, y:Int)
    // …
}



func piece(at p:Point) -> Piece? {
    let (i,j) = p
    // Miejsce na kod sprawdzający pod kątem błędów.
    return self.grid[i][j]
}



func optionalExpecter(_ s:String?) {
    // W tym miejscu nastąpi opakowanie ciągu tekstowego typem opcjonalnym.
    print(s)
}
optionalExpecter("Cześć") // Dane wyjściowe w konsoli to Optional("Cześć").



// self.window to typ opcjonalny opakowujący UIWindow.
self.window!.rootViewController = RootViewController()
self.window!.backgroundColor = UIColor.white
self.window!.makeKeyAndVisible()



// self.window to typ opcjonalny opakowujący UIWindow.
let window = self.window!
// W tym momencie window (nie self.window) jest typu UIWindow, a nie typu opcjonalnego.
window.rootViewController = RootViewController()
window.backgroundColor = UIColor.white
window.makeKeyAndVisible()



var stringMaybe : String? = "Cześć"
print(stringMaybe) // Dane wyjściowe to Optional("Cześć").
if stringMaybe == nil {
    print("Brak wartości.") // Ten komunikat nie zostanie wyświetlony.
}
stringMaybe = nil
print(stringMaybe) // Wartość nil.
if stringMaybe == nil {
    print("Brak wartości.") // Ten komunikat zostanie wyświetlony.
}



var stringMaybe : String?
// W tym miejscu zmienna stringMaybe może mieć przypisaną rzeczywistą wartość.
if stringMaybe != nil {
    let s = stringMaybe!
    // …
}



class Dog {
    var noise : String?
    func speak() -> String? {
        return self.noise
    }
}
let d = Dog()
let bigname = d.speak()?.uppercased()



class ViewController: UIViewController {
    @IBOutlet var myButton: UIButton!
    // …
}



