• Skip to main content

Connections.Digital

Media Production, Mobile Web and iOS Development

  • Facebook
  • X
  • YouTube
  • GitHub
  • Home
  • Media Production
  • Blog
  • Galleries
  • Contact
  • About

November 5, 2018

Intermediate Swift – Chapter 2.3 Structures

import UIKit
import Foundation
//Structures

struct Person {
    var name: String
    func sayHello() {
        print("Hello, there! My name is \(name)")
    }
}

let firstPerson = Person(name: "Kenneth")
print(firstPerson.name)
firstPerson.sayHello()

let person = Person(name: "Zane")
print(person.name)
person.sayHello()

//Instances

//Does not match text example
struct Shirt {
    var size: String
    var color1: String
}

let myShirt = Shirt(size: "xl", color1: "blue")
//let youShirt = Shirt(size: .m, color: .red)

print(myShirt.size)

struct Color {
    var blue: String
    var black: UIColor
    var green: UIColor
}

struct Direction {
    // TODO add stuff
}

struct Car {
    var make: String
    var year: Int
    var color: String
    var topSpeed: Int

    func startEngine() {
        "..."
    }
    func drive() {}
    func park() {}
    func steer(direction: Direction) {}
}

let firstCar = Car(make: "Honda", year: 2010, color: "blue", topSpeed: 120)
let secondCar = Car(make: "Ford", year: 2013, color: "black", topSpeed: 125)

firstCar.startEngine()
firstCar.drive()

struct Odometer {
    var count: Int = 0
}

let odometer = Odometer(count: 27000)
print(odometer.count)

Filed Under: Swift Tagged With: Amarillo, Swift 4.2, Texas

Reader Interactions

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © Connections Information Systems

Designed by Kenneth Jackson at Connections.Digital in Amarillo, Texas

Powered by WPplaces