class Marek {
}
struct Monika {
}
enum Jacek {
}



class Dog {
    var name = ""
    var license = 0
    init(name:String) {
        self.name = name
    }
    init(license:Int) {
        self.license = license
    }
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
}



class Dog {
    var name = ""
    var license = 0
    init() {
    }
    init(name:String) {
        self.name = name
    }
    init(license:Int) {
        self.license = license
    }
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
}



class Dog {
    var name = ""
    var license = 0
    init(name:String = "", license:Int = 0) {
        self.name = name
        self.license = license
    }
}



let fido = Dog(name:"Fido")
let rover = Dog(license:1234)
let spot = Dog(name:"Spot", license:1357)
let puff = Dog()



class Dog {
    var name : String // Brak wartości domyślnej!
    var license : Int // Brak wartości domyślnej!
    init(name:String = "", license:Int = 0) {
        self.name = name
        self.license = license
    }
}



class Dog {
    var name : String
    var license : Int
    init(name:String = "") {
        self.name = name // Błąd w trakcie kompilacji.
    }
}



class Dog {
    let name : String
    let license : Int
    init(name:String = "", license:Int = 0) {
        self.name = name
        self.license = license
    }
}



class Dog {
    let name : String
    let license : Int
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
}



struct Cat {
    var name : String
    var license : Int
    init(name:String, license:Int) {
        self.name = name
        meow() // Zbyt wczesne wywołanie metody — błąd w trakcie kompilacji.
        self.license = license
    }
    func meow() {
        print("miau")
    }
}



struct Digit {
    var number : Int
    var meaningOfLife : Bool
    init(number:Int) {
        self.number = number
        self.meaningOfLife = false
    }
    init() { // To jest delegująca metoda inicjalizacyjna.
        self.init(number:42)
        self.meaningOfLife = true
    }
}



struct Digit { // Nie rób tak!
    var number : Int = 100
    init(value:Int) {
        self.init(number:value)
    }
    init(number:Int) {
        self.init(value:number)
    }
}



class Dog {
    let name : String
    let license : Int
    init?(name:String, license:Int) {
        if name.isEmpty {
            return nil
        }
        if license <= 0 {
            return nil
        }
        self.name = name
        self.license = license
    }
}



class Dog {
    let name : String
    let license : Int
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
}



let fido = Dog(name:"Fido", license:1234)
let spot = Dog(name:"Spot", license:1357)
let aName = fido.name // Dane wyjściowe: "Fido".
let anotherName = spot.name // Dane wyjściowe: "Spot".



struct Greeting {
    static let friendly = "Witaj!"
    static let hostile = "Odejdź!"
}



class Moi {
    let first = "Matt"
    let last = "Neuburg"
    let whole = self.first + " " + self.last // Błąd w trakcie kompilacji.
}



class Moi {
    let first = "Matt"
    let last = "Neuburg"
    var whole : String {
        return self.first + " " + self.last
    }
}



class Moi {
    let first = "Matt"
    let last = "Neuburg"
    lazy var whole = self.first + " " + self.last
}



class Moi {
    let first = "Matt"
    let last = "Neuburg"
    lazy var whole : String = {
        var s = self.first
        s.append(" ")
        s.append(self.last)
        return s
    }()
}



struct Greeting {
    static let friendly = "Witaj!"
    static let hostile = "Odejdź!"
    static let ambivalent = friendly + ", ale " + hostile
}



struct Greeting {
    static let friendly = "Witaj!"
    static let hostile = "Odejdź!"
    static let ambivalent = Greeting.friendly + ", ale " + Greeting.hostile
}



struct Greeting {
    static let friendly = "Witaj!"
    static let hostile = "Odejdź!"
    static var ambivalent : String {
        return self.friendly + ", ale " + self.hostile
    }
}



struct Greeting {
    static let friendly = "Witaj!"
    static let hostile = "Odejdź!"
    static var ambivalent : String = {
        return self.friendly + ", ale " + self.hostile // Błąd w trakcie kompilacji.
    }()
}



class Dog {
    let name : String
    let license : Int
    let whatDogsSay = "hau"
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
    func bark() {
        print(self.whatDogsSay)
    }
    func speak() {
        self.bark()
        print("mam na imię \(self.name)")
    }
}



struct Greeting {
    static let friendly = "Witaj!"
    static let hostile = "Odejdź!"
    static var ambivalent : String {
        return self.friendly + ", ale " + self.hostile
    }
    static func beFriendly() {
        print(self.friendly)
    }
}



class Dog {
    static var whatDogsSay = "hau"
    func bark() {
        print(Dog.whatDogsSay)
    }
}



class MyClass {
    var s = ""
    func store(_ s:String) {
        self.s = s
    }
}
let m = MyClass()
let f = MyClass.store(m) // Co się stało!?



struct Digit {
    var number : Int
    init(_ n:Int) {
        self.number = n
    }
    subscript(ix:Int) -> Int {   // 1. i 2.
        get { // 3.
            let s = String(self.number)
            return Int(String(s[s.index(s.startIndex, offsetBy:ix)]))!
        }
    }
}



struct Digit {
    var number : Int
    init(_ n:Int) {
        self.number = n
    }
    subscript(ix:Int) -> Int {
        get {
            let s = String(self.number)
            return Int(String(s[s.index(s.startIndex, offsetBy:ix)]))!
        }
        set {
            var s = String(self.number)
            let i = s.index(s.startIndex, offsetBy:ix)
            s.replaceSubrange(i...i, with: String(newValue))
            self.number = Int(s)!
        }
    }
}



class Dog {
    struct Noise {
        static var noise = "hau"
    }
    func bark() {
        print(Dog.Noise.noise)
    }
}



enum Filter {
    case albums
    case playlists
    case podcasts
    case books
}



func filterExpecter(_ type:Filter) {
    if type == .albums {
        print("To są albumy.")
    }
}
filterExpecter(.albums) // Dane wyjściowe: "To są albumy.".



enum PepBoy : Int {
    case manny
    case moe
    case jack
}



enum Filter : String {
    case albums
    case playlists
    case podcasts
    case books
}



enum Normal : Double {
    case fahrenheit = 98.6
    case centigrade = 37
}
enum PepBoy : Int {
    case manny = 1
    case moe // Niejawnie przypisaną wartością jest 2.
    case jack = 4
}
enum Filter : String {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
}



struct Thing : RawRepresentable {
    let rawValue : Int
    static let one : Thing = Thing(rawValue:1)
    static let two : Thing = Thing(rawValue:2)
}



enum MyError {
    case number(Int)
    case message(String)
    case fatal
}



enum MyError2 {
    case number(Int)
    case message(String)
    case fatal(n:Int, s:String)
}
let err : MyError2 = .fatal(n:-12, s:"Co za horror.")



enum MyError : Equatable { // *
    case number(Int)
    case message(String)
    case fatal
}



enum Filter : String {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    static let cases : [Filter] = [.albums, .playlists, .podcasts, .books]
}



enum Filter : String, CaseIterable {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    // Właściwość allCases ma teraz wartość [.albums, .playlists, .podcasts, .books].
}



enum Filter : String, CaseIterable {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    init(_ ix:Int) {
        self = Filter.allCases[ix]
    }
}



enum Filter : String, CaseIterable {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    init?(_ ix:Int) {
        if !Filter.allCases.indices.contains(ix) {
            return nil
        }
        self = Filter.allCases[ix]
    }
}



enum Filter : String, CaseIterable {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    init?(_ ix:Int) {
        if !Filter.allCases.indices.contains(ix) {
            return nil
        }
        self = Filter.allCases[ix]
    }
    init?(_ rawValue:String) {
        self.init(rawValue:rawValue)
    }
}



enum Filter : String {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    var query : MPMediaQuery {
        switch self {
        case .albums:
            return .albums()
        case .playlists:
            return .playlists()
        case .podcasts:
            return .podcasts()
        case .books:
            return .audiobooks()
        }
    }
}



enum Shape {
    case rectangle
    case ellipse
    case diamond
    func addShape (to p: CGMutablePath, in r: CGRect) -> () {
        switch self {
        case .rectangle:
            p.addRect(r)
        case .ellipse:
            p.addEllipse(in:r)
        case .diamond:
            p.move(to: CGPoint(x:r.minX, y:r.midY))
            p.addLine(to: CGPoint(x: r.midX, y: r.minY))
            p.addLine(to: CGPoint(x: r.maxX, y: r.midY))
            p.addLine(to: CGPoint(x: r.midX, y: r.maxY))
            p.closeSubpath()
        }
    }
}



enum Filter : String, CaseIterable {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    mutating func advance() {
        var ix = Filter.allCases.firstIndex(of:self)!
        ix = (ix + 1) % Filter.allCases.count
        self = Filter.allCases[ix]
    }
}



enum InterfaceMode : Int {
    case timed = 0
    case practice = 1
}



var interfaceMode : InterfaceMode = .timed {
    willSet (mode) {
        self.timedPractice?.selectedSegmentIndex = mode.rawValue
    }
}



struct Digit {
    var number = 42
    init(number:Int) {
        self.number = number
    }
}



struct Digit {
    var number = 42
    init() {}
    init(number:Int) {
        self.number = number
    }
}



struct Default {
    static let rows = "CardMatrixRows"
    static let columns = "CardMatrixColumns"
    static let hazyStripy = "HazyStripy"
}



struct Digit {
    var number : Int
    init(_ n:Int) {
        self.number = n
    }
}



var d : Digit = Digit(123) {
    didSet {
        print("Przypisano wartość zmiennej d.")
    }
}
d.number = 42 // Dane wyjściowe: "Przypisano wartość zmiennej d.".



struct Digit {
    var number : Int
    init(_ n:Int) {
        self.number = n
    }
}
let d = Digit(123)
d.number = 42 // Błąd w trakcie kompilacji.



struct Digit {
    var number : Int
    init(_ n:Int) {
        self.number = n
    }
    mutating func changeNumberTo(_ n:Int) {
        self.number = n
    }
}



class Dog {
    var name : String = "Fido"
}
let rover = Dog()
rover.name = "Rover" // To polecenie działa bez problemu.



var rover : Dog = Dog() {
    didSet {
        print("Przypisanie wartości właściwości.")
    }
}
rover.name = "Rover" // Żaden komunikat nie zostanie wygenerowany w konsoli.



struct Digit {
    var number : Int
    init(_ n:Int) {
        self.number = n
    }
    mutating func changeNumberTo(_ n:Int) {
        self.number = n
    }
    mutating func callAnotherFunction() {
        otherFunction {
            self.changeNumberTo(345)
        }
    }
}



var d = Digit(123)
print(d.number) // Dane wyjściowe: 123.
var d2 = d // Przypisanie!
d2.number = 42
print(d.number) // Dane wyjściowe: 123.



var fido = Dog()
print(fido.name) // Dane wyjściowe: "Fido".
var rover = fido // Przypisanie!
rover.name = "Rover"
print(fido.name) // Dane wyjściowe: "Rover".



func dogChanger(_ d:Dog) {
    d.name = "Rover"
}
var fido = Dog()
print(fido.name) // Dane wyjściowe: "Fido".
dogChanger(fido)
print(fido.name) // Dane wyjściowe: "Rover".



enum Node {
    case none(Int)
    indirect case left(Int, Node)
    indirect case right(Int, Node)
    indirect case both(Int, Node, Node)
}



let countedGreet = countAdder(greet)
let countedGreet2 = countAdder(greet)
countedGreet() // Wartość licznika wynosi 1.
countedGreet2() // Wartość licznika wynosi 1.



let countedGreet = countAdder(greet)
let countedGreet2 = countedGreet
countedGreet() // Wartość licznika wynosi 1.
countedGreet2() // Wartość licznika wynosi 2.



class Quadruped {
    func walk () {
        print("spacer, spacer, spacer")
    }
}
class Dog : Quadruped {}
class Cat : Quadruped {}



class Quadruped {
    func walk () {
        print("spacer, spacer, spacer")
    }
}
class Dog : Quadruped {
    func bark () {
        print("hau")
    }
}



class Quadruped {
    func walk () {
        print("spacer, spacer, spacer")
    }
}
class Dog : Quadruped {
    func bark () {
        print("hau")
    }
    func barkAndWalk() {
        self.bark()
        self.walk()
    }
}



class Quadruped {
    func walk () {
        print("spacer, spacer, spacer")
    }
}
class Dog : Quadruped {
    func bark () {
        print("hau")
    }
}
class NoisyDog : Dog {
    override func bark () {
        print("hau hau hau")
    }
}



class Dog {
    func barkAt(cat:Kitten) {}
}
class NoisyDog : Dog {
    override func barkAt(cat:Cat) {} // Czy może Cat?
}



class Dog {
    func barkAt(cat:Cat) {} // Czy może Kitten?
}
class NoisyDog : Dog {
    override func barkAt(cat:Cat?) {}
}



class Dog : Quadruped {
    func bark () {
        print("hau")
    }
}
class NoisyDog : Dog {
    override func bark () {
        for _ in 1...3 {
            super.bark()
        }
    }
}



let fido = Dog()
fido.bark() // Dane wyjściowe: hau.
let rover = NoisyDog()
rover.bark() // Dane wyjściowe: hau hau hau.



class Dog {
    var name = "Fido"
}
let d = Dog()



class Dog {
    var name = "Fido"
    init(name:String) {self.name = name}
}
let d = Dog(name:"Rover") // Tak można zrobić.
let d2 = Dog() // Błąd w trakcie kompilacji.



class Dog {
    var name = "Fido"
    convenience init(name:String) {
        self.init()
        self.name = name
    }
}
let d = Dog(name:"Rover")
let d2 = Dog()



class Dog {
    var name : String
    var license : Int
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
}
let d = Dog(name:"Rover", license:42)



class Dog {
    var name : String
    var license : Int
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
    convenience init(license:Int) {
        self.init(name:"Fido", license:license)
    }
    convenience init() {
        self.init(license:1)
    }
}
let d = Dog()



class Dog {
    var name : String
    var license : Int
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
    convenience init(license:Int) {
        self.init(name:"Fido", license:license)
    }
}
class NoisyDog : Dog {
}



class Dog {
    var name : String
    var license : Int
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
    convenience init(license:Int) {
        self.init(name:"Fido", license:license)
    }
}
class NoisyDog : Dog {
    convenience init(name:String) {
        self.init(name:name, license:1)
    }
}



class Dog {
    var name : String
    var license : Int
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
    convenience init(license:Int) {
        self.init(name:"Fido", license:license)
    }
}
class NoisyDog : Dog {
    init(name:String) {
        super.init(name:name, license:1)
    }
}



class Dog {
    let name : String
    init(name:String) {
        self.name = name
    }
}
class RoverDog : Dog {
    init() {
        super.init(name:"Rover")
    }
}
let fido = RoverDog(name:"Fido") // Błąd w trakcie kompilacji.



class Dog {
    var name : String
    var license : Int
    init(name:String, license:Int) {
        self.name = name
        self.license = license
    }
    convenience init(license:Int) {
        self.init(name:"Fido", license:license)
    }
}
class NoisyDog : Dog {
    override init(name:String, license:Int) {
        super.init(name:name, license:license)
    }
}



class Dog {
    var name : String
    required init(name:String) {
        self.name = name
    }
}
class NoisyDog : Dog {
    var obedient = false
    init(obedient:Bool) {
        self.obedient = obedient
        super.init(name:"Fido")
    }
} // Błąd w trakcie kompilacji.



class Dog {
    var name : String
    required init(name:String) {
        self.name = name
    }
}
class NoisyDog : Dog {
    var obedient = false
    init(obedient:Bool) {
        self.obedient = obedient
        super.init(name:"Fido")
    }
    required init(name:String) {
        super.init(name:name)
    }
}



class Dog {
    static func whatDogsSay() -> String {
        return "hau"
    }
    func bark() {
        print(Dog.whatDogsSay())
    }
}



class Dog {
    class func whatDogsSay() -> String {
        return "hau"
    }
    func bark() {
        print(Dog.whatDogsSay())
    }
}



class NoisyDog : Dog {
    override class func whatDogsSay() -> String {
        return "HAU"
    }
}



class Dog {
    static var whatDogsSay = "hau"
    func bark() {
        print(Dog.whatDogsSay)
    }
}



class Dog {
    class var whatDogsSay : String {
        return "hau"
    }
    func bark() {
        print(Dog.whatDogsSay)
    }
}



class NoisyDog : Dog {
    override static var whatDogsSay : String {
        return "HAU"
    }
}



class Dog {
}
class NoisyDog : Dog {
}
let d : Dog = NoisyDog()



class Dog {
    func bark() {
        print("hau")
    }
}
class NoisyDog : Dog {
    override func bark() {
        for _ in 1...3 {
            super.bark()
        }
    }
}



func tellToBark(_ d:Dog) {
    d.bark()
}
var nd = NoisyDog()
tellToBark(nd)



func tellToBark(_ d:Dog) {
    d.bark()
}
var nd = NoisyDog()
tellToBark(nd) // Dane wyjściowe: hau hau hau.



class Dog {
    func bark() {
        print("hau")
    }
    func speak() {
        self.bark()
    }
}
class NoisyDog : Dog {
    override func bark() {
        for _ in 1...3 {
            super.bark()
        }
    }
}



class Dog {
    func bark() {
        print("hau")
    }
}
class NoisyDog : Dog {
    override func bark() {
        super.bark(); super.bark()
    }
    func beQuiet() {
        self.bark()
    }
}



func tellToHush(_ d:Dog) {
    d.beQuiet() // Błąd w trakcie kompilacji.
}
let nd = NoisyDog()
tellToHush(nd)



func tellToHush(_ d:Dog) {
    (d as! NoisyDog).beQuiet()
}
let nd = NoisyDog()
tellToHush(nd)



func tellToHush(_ d:Dog) {
    let d2 = d as! NoisyDog
    d2.beQuiet()
    // Inne komunikaty NoisyDog wysyłane do d2.
}
let nd = NoisyDog()
tellToHush(nd)



func tellToHush(_ d:Dog) {
    (d as! NoisyDog).beQuiet() // Kod zostanie skompilowany, ale bądź gotowy na awarię aplikacji!
}
let d = Dog()
tellToHush(d)



func tellToHush(_ d:Dog) {
    if d is NoisyDog {
        let d2 = d as! NoisyDog
        d2.beQuiet()
    }
}



func tellToHush(_ d:Dog) {
    let noisyMaybe = d as? NoisyDog // Wartość typu opcjonalnego opakowująca NoisyDog.
    if noisyMaybe != nil {
        noisyMaybe!.beQuiet()
    }
}



let s : NSString = "cześć"         // Przypisanie literału ciągu tekstowego stałej typu NSString.
let s2 = "cześć"
let s3 : NSString = s2 as NSString // Rzutowanie wartości typu String na NSString.
let i : NSNumber = 1 as NSNumber   // Rzutowanie wartości typu Int na NSNumber.



class Dog {
    class var whatDogsSay : String {
        return "hau"
    }
    func bark() {
        print(Dog.whatDogsSay)
    }
}



class Dog {
    class var whatDogsSay : String {
        return "hau"
    }
    func bark() {
        print(type(of:self).whatDogsSay)
    }
}



class Dog {
    class var whatDogsSay : String {
        return "hau"
    }
    func bark() {
        print(type(of:self).whatDogsSay)
    }
}
class NoisyDog : Dog {
    override class var whatDogsSay : String {
        return "hau hau hau"
    }
}



class Dog {
    var name : String
    init(name:String) {
        self.name = name
    }
}
class NoisyDog : Dog {
}



func dogMakerAndNamer(_ whattype:Dog.Type) -> Dog {
    let d = whattype.init(name:"Fido") // Błąd w trakcie kompilacji.
    return d
}



class Dog {
    var name : String
    required init(name:String) {
        self.name = name
    }
}
class NoisyDog : Dog {
}



class Dog {
    var name : String
    required init(name:String) {
        self.name = name
    }
    class func makeAndName() -> Dog {
        let d = self.init(name:"Fido")
        return d
    }
}
class NoisyDog : Dog {
}



class Dog {
    var name : String
    required init(name:String) {
        self.name = name
    }
    class func makeAndName() -> Self {
        let d = self.init(name:"Fido")
        return d
    }
}
class NoisyDog : Dog {
}



class Dog {
    var name : String
    required init(name:String) {
        self.name = name
    }
    func havePuppy(name:String) -> Self {
        return type(of:self).init(name:name)
    }
}
class NoisyDog : Dog {
}



let d = Dog(name:"Fido")
let d2 = d.havePuppy(name:"Fido jr.")
let nd = NoisyDog(name:"Rover")
let nd2 = nd.havePuppy(name:"Rover jr.")



func dogTypeExpecter(_ whattype:Dog.Type) {
    let equality = whattype == Dog.self
    let typology = whattype is Dog.Type
}



protocol Flier {
    func fly()
}
struct Bird : Flier {
    func fly() {
    }
}



func tellToFly(_ f:Flier) {
    f.fly()
}
struct Bee {
    func fly() {
    }
}
let b = Bee()
tellToFly(b) // Błąd w trakcie kompilacji.



func tellToFly(_ f:Flier) {
    f.fly()
}
struct Bee : Flier {
    func fly() {
    }
}
let b = Bee()
tellToFly(b)



enum Filter : String {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    var description : String { return self.rawValue }
}



enum Filter : String, CustomStringConvertible {
    case albums = "Albumy"
    case playlists = "Listy odtwarzania"
    case podcasts = "Podcasty"
    case books = "Audiobooki"
    var description : String { return self.rawValue }
}



let type = Filter.albums
print("\(type)") // Dane wyjściowe: Albumy.
print(type) // Dane wyjściowe: Albumy.
let s = String(describing:type) // Dane wyjściowe: Albumy.



struct Bird : Flier {
    func fly() {
    }
    func getWorm() {
    }
}



protocol MyViewProtocol : class {
    func doSomethingCool()
}
class ViewController: UIViewController {
    var v: (UIView & MyViewProtocol)?
    // …
}



@objc protocol Flier {
    @objc optional var song : String {get}
    @objc optional func sing()
}



class Bird : Flier {
    func sing() {
        print("tweet")
    }
}



@objc protocol Flier {
    @objc optional var song : String? {get}
    @objc optional func sing()
}
let f : Flier = Bird()
let s = f.song // Tutaj s to typ opcjonalny opakowujący typ opcjonalny, który z kolei zawiera wartość typu String.



@objc protocol Flier {
    @objc optional var song : String {get}
    @objc optional func sing() -> String
}



class SecondViewController : UIViewController {
    weak var delegate : SecondViewControllerDelegate?
    // …
}



class SecondViewController : UIViewController {
    weak var delegate : (NSObject & SecondViewControllerDelegate)?
    // …
}



protocol Flier {
    init()
}
class Bird : Flier {
    init() {} // Błąd w trakcie kompilacji.
}



protocol Flier {
    init()
}
class Bird : Flier {
    required init() {}
}



class ViewController: UIViewController {
    init() {
        super.init(nibName: "ViewController", bundle: nil)
    }
}



struct Nest : ExpressibleByIntegerLiteral {
    var eggCount : Int = 0
    init() {}
    init(integerLiteral val: Int) {
        self.eggCount = val
    }
}



func reportEggs(_ nest:Nest) {
    print("To gniazdo zawiera \(nest.eggCount) jaja.")
}
reportEggs(4) // Dane wyjściowe: To gniazdo zawiera 4 jaja.



func dogMakerAndNamer(_ whattype:Dog.Type) -> Dog {
    let d = whattype.init(name:"Fido")
    return d
}



func dogMakerAndNamer<WhatType:Dog>(_:WhatType.Type) -> WhatType {
    let d = WhatType.init(name:"Fido")
    return d
}



func dogMakerAndNamer(_:NoisyDog.Type) -> NoisyDog {
    let d = NoisyDog.init(name:"Fido")
    return d
}



enum Optional<Wrapped> : ExpressibleByNilLiteral {
    case none
    case some(Wrapped)
    init(_ some: Wrapped)
    // …
}



enum Optional<String> {
    case none
    case some(String)
    init(_ some: String)
    // …
}



protocol Flier {
    associatedtype Other
    func flockTogetherWith(_ f:Other)
    func mateWith(_ f:Other)
}



struct Bird : Flier {
    func flockTogetherWith(_ f:Bird) {}
    func mateWith(_ f:Bird) {}
}



struct HolderOfTwoSameThings<T> {
    var firstThing : T
    var secondThing : T
    init(thingOne:T, thingTwo:T) {
        self.firstThing = thingOne
        self.secondThing = thingTwo
    }
}



func takeAndReturnSameThing<T> (_ t:T) -> T {
    if T.self is String.Type {
        // …
    }
    return t
}



func dogMakerAndNamer<WhatType:Dog>(_:WhatType.Type) -> WhatType {
    let d = WhatType.init(name:"Fido")
    return d
}



protocol Flier {
    associatedtype Other
    func flockTogetherWith(_ f:Other)
    func mateWith(_ f:Other)
}



struct Bird : Flier {
    func flockTogetherWith(_ f: String) {}
    func mateWith(_ f:String) {}
}



struct Bird : Flier { // Błąd w trakcie kompilacji.
    func flockTogetherWith(_ f: String) {}
    func mateWith(_ f:Int) {}
}



protocol Flier {
    func fly()
}
protocol Flocker {
    associatedtype Other : Flier // *
    func flockTogetherWith(f:Other)
}
struct Bird : Flocker, Flier {
    func fly() {}
    func flockTogetherWith(f:Bird) {}
}



struct Bird : Flocker, Flier {
    func fly() {}
    func flockTogetherWith(f:Flier) {}
}



func myMin<T>(_ things:T...) -> T {
    var minimum = things.first!
    for item in things.dropFirst() {
        if item < minimum { // Błąd w trakcie kompilacji.
            minimum = item
        }
    }
    return minimum
}



protocol Flier {
    associatedtype Other
}
struct Bird : Flier {
    typealias Other = String
}



class Dog<T> {
    var name : T?
}
let d = Dog<String>()



func dogMakerAndNamer<WhatType:Dog>(_:WhatType.Type) -> WhatType {
    let d = WhatType.init(name:"Fido")
    return d
}



protocol Flier {
    init()
}
struct Bird : Flier {
    init() {}
}
struct FlierMaker<T:Flier> {
    static func makeFlier() -> T {
        return T()
    }
}
let f = FlierMaker<Bird>.makeFlier() // Wartością zwrotną jest egzemplarz typu Bird.



struct Wrapper<T> {
}
class Cat {
}
class CalicoCat : Cat {
}



protocol Meower {
    func meow()
}
struct Wrapper<T:Meower> {
    let meower : T
}
class Cat : Meower {
    func meow() { print("miau") }
}
class CalicoCat : Cat {
}



struct Soldier : Fighter {
    typealias Enemy = Archer
}
struct Archer : Fighter {
    typealias Enemy = Soldier
}



protocol Wieldable {
}
struct Sword : Wieldable {
}
struct Bow : Wieldable {
}



protocol Fighter {
    associatedtype Enemy : Fighter
    associatedtype Weapon : Wieldable
}
struct Soldier : Fighter {
    typealias Weapon = Sword
    typealias Enemy = Archer
}
struct Archer : Fighter {
    typealias Weapon = Bow
    typealias Enemy = Soldier
}



protocol Fighter {
    associatedtype Enemy : Fighter
    associatedtype Weapon : Wieldable
    func steal(weapon:Self.Enemy.Weapon, from:Self.Enemy)
}



struct Soldier : Fighter {
    typealias Weapon = Sword
    typealias Enemy = Archer
    func steal(weapon:Bow, from:Archer) {
    }
}
struct Archer : Fighter {
    typealias Weapon = Bow
    typealias Enemy = Soldier
    func steal(weapon:Sword, from:Soldier) {
    }
}



func flyAndWalk<T: Flier> (_ f:T) {}
func flyAndWalk2<T: Flier & Walker> (_ f:T) {}
func flyAndWalk3<T: Flier & Dog> (_ f:T) {}



func flyAndWalk<T> (_ f:T) where T: Flier {}
func flyAndWalk2<T> (_ f:T) where T: Flier & Walker {}
func flyAndWalk2a<T> (_ f:T) where T: Flier, T: Walker {}
func flyAndWalk3<T> (_ f:T) where T: Flier & Dog {}
func flyAndWalk3a<T> (_ f:T) where T: Flier, T: Dog {}



protocol Flier {
    associatedtype Other
}
func flockTogether<T> (_ f:T) where T:Flier, T.Other /* ... */ {}



protocol Flier {
    associatedtype Other
}
struct Bird : Flier {
    typealias Other = String
}
struct Insect : Flier {
    typealias Other = Bird
}
func flockTogether<T> (_ f:T) where T:Flier, T.Other:Equatable {}



protocol Flier {
    associatedtype Other
}
struct Bird : Flier {
    typealias Other = String
}
struct Insect : Flier {
    typealias Other = Int
}
func flockTwoTogether<T,U> (_ f1:T, _ f2:U)
    where T:Flier, U:Flier, T.Other == U.Other {}



protocol Sequence {
    associatedtype Element where Self.Element == Self.Iterator.Element
    // …
}



extension CGRect {
    var center : CGPoint {
        return CGPoint(x:self.midX, y:self.midY)
    }
}



extension String {
    func range(_ start:Int, _ count:Int) -> Range<String.Index> {
        let i = self.index(start >= 0 ?
            self.startIndex :
            self.endIndex, offsetBy: start)
        let j = self.index(i, offsetBy: count)
        return i..<j
    }
}



let s = "abcdefg"
let r1 = s.range(2,2)
let r2 = s.range(-3,2)
print(s[r1]) // Dane wyjściowe: cd.
print(s[r2]) // Dane wyjściowe: ef.



extension UIColor {
    class var myGolden : UIColor {
        return self.init(
            red:1.000, green:0.894, blue:0.541, alpha:0.900
        )
    }
}



class ViewController: UIViewController {
    // To jest miejsce na nadpisanie metody UIViewController.
}
extension ViewController : UIPopoverPresentationControllerDelegate {
    // To jest miejsce na nadpisanie metody UIPopoverPresentationControllerDelegate.
}
extension ViewController : UIToolbarDelegate {
    // To jest miejsce na nadpisanie metody UIToolbarDelegate.
}



struct Digit {
    var number : Int
}
extension Digit {
    init() {
        self.init(number:42)
    }
}



protocol Flier {
}
extension Flier {
    func fly() {
        print("fru fru fru")
    }
}
struct Bird : Flier {
}



protocol Flier {
}
extension Flier {
    func fly() {
        print("fru fru fru")
    }
}
struct Insect : Flier {
    func fly() {
        print("bzz")
    }
}
let i = Insect()
i.fly() // Dane wyjściowe: bzz.



protocol Flier {
    func fly() // *
}
extension Flier {
    func fly() {
        print("fru fru fru")
    }
}
struct Insect : Flier {
    func fly() {
        print("bzz")
    }
}



class Dog<T> {
    var name : T?
}
extension Dog {
    func sayYourName() -> T? { // T? to typ self.name.
        return self.name
    }
}



func myMin<T:Comparable>(_ things:T...) -> T {
    var minimum = things.first!
    for item in things.dropFirst() {
        if item < minimum {
            minimum = item
        }
    }
    return minimum
}



extension Array where Element:Comparable {
    func myMin() -> Element? {
        var minimum = self.first
        for item in self.dropFirst() {
            if item < minimum! {
                minimum = item
            }
        }
        return minimum
    }
}



func anyExpecter(_ a:Any) {}
anyExpecter("cześć")     // Egzemplarz  struktury.
anyExpecter(String.self) // Typ struktury.
anyExpecter(Dog())       // Egzemplarz klasy.
anyExpecter(Dog.self)    // Typ klasy.
anyExpecter(anyExpecter) // Funkcja.



if anything is String {
    let s = anything as! String
    // …
}



let t = Thing()
t.take1id("cześć")  // Konwersja String na NSString.
t.take1id(1)        // Konwersja Int na NSNumber.
t.take1id(CGRect()) // Konwersja CGRect na NSValue.
t.take1id(Date())   // Konwersja Date na NSDate.
t.take1id(Bird())   // Konwersja Bird (struktura) na typ opakowany.



let ud = UserDefaults.standard
let d = ud.object(forKey:"now")
if d is Date {
    let d = d as! Date
    // …
}



class Dog {
}
let d = Dog()
let anyo : AnyObject = d
let d2 = anyo as! Dog



let s = "cześć" as AnyObject  // Konwersja String na NSString, a później na AnyObject.
let i = 1 as AnyObject        // Konwersja Int na NSNumber, a później na AnyObject.
let r = CGRect() as AnyObject // Konwersja CGRect na NSValue, a później na AnyObject.
let d = Date() as AnyObject   // Konwersja Date na NSDate, a później na AnyObject.
let b = Bird() as AnyObject   // Konwersja Bird (struktura) na typ opakowany, a później na AnyObject.



class Dog {
    @objc var noise : String = "hau"
    @objc func bark() -> String {
        return "hau"
    }
}
class Cat {}



@objc func changed(_ n:Notification) {
    let player = MPMusicPlayerController.applicationMusicPlayer
    if n.object as AnyObject === player {
        // …
    }
}



class Dog {
    @objc static var whatADogSays : String = "hau"
}
class Cat {}



let dog1 : Dog = NoisyDog()
let dog2 : Dog = NoisyDog()
let arr = [dog1, dog2]
let arr2 = arr as! [NoisyDog]



let dog1 : Dog = NoisyDog()
let dog2 : Dog = NoisyDog()
let dog3 : Dog = Dog()
let arr = [dog1, dog2]
let arr2 = arr as? [NoisyDog] // Typ opcjonalny opakowujący tablicę elementów NoisyDog.
let arr3 = [dog2, dog3]
let arr4 = arr3 as? [NoisyDog] // Dane wyjściowe: nil.



let i1 = 1
let i2 = 2
let i3 = 3
let arr : [Int] = [1,2,3]
if arr == [i1,i2,i3] { // Te tablice są równe!



let nd1 = NoisyDog()
let d1 = nd1 as Dog
let nd2 = NoisyDog()
let d2 = nd2 as Dog
if [d1,d2] == [nd1,nd2] { // Te tablice są równe!



var arr = [1,2,3]
arr[1..<2] = [7,8] // arr ma teraz postać [1,7,8,3].
arr[1..<2] = []    // arr ma teraz postać [1,8,3].
arr[1..<1] = [10]  // arr ma teraz postać [1,10,8,3] (żaden element nie został usunięty!).
let arr2 = [20,21]
// arr[1..<1] = arr2 // Błąd w trakcie kompilacji. Trzeba użyć następującego polecenia:
arr[1..<1] = ArraySlice(arr2) // arr ma teraz postać [1,20,21,10,8,3].



let arr = [1,2,3]
let slice = arr.suffix(from:1)     // Dane wyjściowe: [2,3].
let slice2 = arr[1...]             // Dane wyjściowe: [2,3].
let slice3 = arr.prefix(upTo:1)    // Dane wyjściowe: [1].
let slice4 = arr.prefix(through:1) // Dane wyjściowe: [1,2].



var arr = [1,2,3]
arr.append(4)
arr.append(contentsOf:[5,6])
arr.append(contentsOf:7...8) // arr ma teraz postać [1,2,3,4,5,6,7,8].



let arr = [1,2,3]
let arr2 = arr + [4] // arr2 ma teraz postać [1,2,3,4].
var arr3 = [1,2,3]
arr3 += [4] // arr3 ma teraz postać [1,2,3,4].



let pepboys = ["Marek", "Monika", "Jacek"]
for pepboy in pepboys {
    print(pepboy) // Dane wyjściowe: Marek, następnie Monika i później Jacek.
}



let pepboys = ["Marek", "Monika", "Jacek"]
for (ix,pepboy) in pepboys.enumerated() {
    print("Osoba nr \(ix) to \(pepboy)") // Dane wyjściowe: Osoba nr 0 to Marek itd.
}
// Ewentualnie:
pepboys.enumerated().forEach {
    print("Osoba nr \($0.offset) to \($0.element)")
}



let pepboys = ["Marek", "Jacek", "Monika"]
let arr1 = pepboys.filter{$0.hasPrefix("M")} // Dane wyjściowe: ["Marek", "Monika"].
let arr2 = pepboys.prefix{$0.hasPrefix("M")} // Dane wyjściowe: ["Marek"].
let arr3 = pepboys.drop{$0.hasPrefix("M")} // Dane wyjściowe: ["Jacek", "Monika"].



let nums = [1,3,2,4,5]
let result = nums.reduce(into: [[],[]]) { temp, i in
    temp[i%2].append(i)
}
// Stała result ma teraz wartość [[2, 4], [1, 3, 5]].



let arr = [["Marek", "Monika", "Jacek"], ["Harpo", "Chico", "Groucho"]]
let target = "m"
let arr2 = arr.map {
    $0.filter {
        let found = $0.range(of:target, options:.caseInsensitive)
        return (found != nil)
    }
}.filter {$0.count > 0}
// Dane wyjściowe: [["Marek", "Monika"]].



var arr = ["Marek", "Monika", "Jacek"]
let arr2 = NSMutableArray(array:arr)
arr2.remove("Monika")
arr = arr2 as! [String]



@implementation Pep
- (NSArray*) boys {
    return @[@"Marek", @"Monika", @"Jacek"];
}
@end



let r = 1...
let names = ["Kalifornia", "Nowy Jork"]
let d = Dictionary(uniqueKeysWithValues: zip(r,names))
// Dane wyjściowe: [2: "Nowy Jork", 1: "Kalifornia"].



var sectionNames = [String]()
var cellData = [[String]]()
var previous = ""
for aState in states {
    // Pobranie pierwszej litery.
    let c = String(aState.prefix(1))
    // Dodanie litery do sectionNames tylko wtedy, gdy jest inną literą.
    if c != previous {
        previous = c
        sectionNames.append(c.uppercased())
        // W tym przypadku następuje dodanie nowej podtablicy do tablicy podtablic.
        cellData.append([String]())
    }
    cellData[cellData.count-1].append(aState)
}
let d = Dictionary(uniqueKeysWithValues: zip(sectionNames,cellData))
// Dane wyjściowe: ["H": ["Hawaje"], "V": ["Vermont"], …].



var d = ["CA": "Kalifornia", "NY": "Nowy Jork"]
d["CA"] = "Casablanca"
d["MD"] = "Maryland"
// Słownik d ma teraz postać ["MD": "Maryland", "NY": "Nowy Jork", "CA": "Casablanca"].



var d = [String:Int]()
for word in words {
    let ct = d[word]
    if ct != nil {
        d[word]! += 1
    } else {
        d[word] = 1
    }
}
// Słownik d ma teraz postać ["Gdy": 1, "pomoże": 1, "pomoże,": 1, "nie": 1, "może": 1, "Pomorze": 1, "to": 1, "morze.": 1].



let dog1 : Dog = NoisyDog()
let dog2 : Dog = NoisyDog()
let d = ["fido": dog1, "rover": dog2]
let d2 = d as! [String : NoisyDog]



var d = ["CA": "Kalifornia", "NY": "Nowy Jork"]
for s in d.keys {
    print(s) // Dane wyjściowe: NY, następnie CA.
}



let d : [String:Int] = ["jeden":1, "dwa":2, "trzy":3]
let keysSorted = d.keys.sorted() // Dane wyjściowe: ["jeden", "trzy", "dwa"].
let arr = d.values.filter{$0 < 2} // Dane wyjściowe: [1].
let min = d.values.min() // Dane wyjściowe: Optional(1).
let sum = d.values.reduce(0, +) // Dane wyjściowe: 6.
let ok = d.keys == ["jeden":1, "trzy":3, "dwa":2].keys // Dane wyjściowe: true.



var d = ["CA": "Kalifornia", "NY": "Nowy Jork"]
for (abbrev, state) in d {
    print("\(abbrev) oznacza stan \(state)")
}



var d = ["CA": "Kalifornia", "NY": "Nowy Jork"]
for pair in d {
    print("\(pair.key) oznacza stan \(pair.value)")
}



let d1 = ["CA": "Kalifornia", "NY": "Nowy Jork"]
let d2 = ["MD": "Maryland", "NY": "Nowy Jork"]
let d3 = d1.merging(d2){orig, _ in orig}
// Dane wyjściowe: ["MD": "Maryland", "NY": "Nowy Jork", "CA": "Kalifornia"].



let prog = n.userInfo?["progress"] as? Double
if prog != nil {
    self.progress = prog!
}



UINavigationBar.appearance().titleTextAttributes = [
    .font: UIFont(name: "ChalkboardSE-Bold", size: 20)!,
    .foregroundColor: UIColor.darkText,
    .shadow.: {
        let shad = NSShadow()
        shad.shadowOffset = CGSize(width:1.5,height:1.5)
        return shad
    }()
]



var d1 = ["NY": "Nowy Jork", "CA": "Kalifornia"]
let d2 = ["MD": "Maryland"]
let mutd1 = NSMutableDictionary(dictionary:d1)
mutd1.addEntries(from:d2)
d1 = mutd1 as! [String:String]
// Egzemplarz d1 ma teraz postać ["MD": "Maryland", "NY": "Nowy Jork", "CA": "Kalifornia"].



struct Person : Hashable {
    let firstName: String
    let lastName: String
}



var set : Set = [Dog(name:"Fido", license:1)]
let d = Dog(name:"Fido", license:2)
set.insert(d)      // Dane wyjściowe: [Dog(name: "Fido", license: 1)].
set.update(with:d) // Dane wyjściowe: [Dog(name: "Fido", license: 2)].



let ud = UserDefaults.standard
var recents = ud.object(forKey:Defaults.recents) as? [Int]
if recents == nil {
    recents = []
}
var forbiddenNumbers = Set(recents!)
let legalNumbers = Set(1...PIXCOUNT).subtracting(forbiddenNumbers)
let newNumber = legalNumbers.randomElement()!
forbiddenNumbers.insert(newNumber)
ud.set(Array(forbiddenNumbers), forKey:Defaults.recents)



typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
    UIViewAnimationOptionLayoutSubviews            = 1 << 0,
    UIViewAnimationOptionAllowUserInteraction      = 1 << 1,
    UIViewAnimationOptionBeginFromCurrentState     = 1 << 2,
    UIViewAnimationOptionRepeat                    = 1 << 3,
    UIViewAnimationOptionAutoreverse               = 1 << 4,
    // …
};



UIView.AnimationOptions.layoutSubviews          0b00000001
UIView.AnimationOptions.allowUserInteraction    0b00000010
UIView.AnimationOptions.beginFromCurrentState   0b00000100
UIView.AnimationOptions.repeat                  0b00001000
UIView.AnimationOptions.autoreverse             0b00010000



let val =
    UIView.AnimationOptions.autoreverse.rawValue |
    UIView.AnimationOptions.repeat.rawValue
let opts = UIView.AnimationOptions(rawValue: val)



override func didTransition(to state: UITableViewCell.StateMask) {
    let editing = UITableViewCell.StateMask.showingEditControl.rawValue
    if state.rawValue & editing != 0 {
        // Włączony jest bit ShowingEditControl.
    }
}



override func didTransition(to state: UITableViewCell.StateMask) {
    if state.contains(.showingEditControl) {
        // Włączony jest bit ShowingEditControl.
    }
}



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let t = touches.first // Typ opcjonalny opakowujący UITouch.
    // …
}



