struct Resolusi {
var width = 0
var height = 0
}
class VideoMode {
var resolution = Resolusi()
var interlaced = false
var frameRate = 0.0
var name: String? = ""
}
// inisialisasi class/struct ke konstanta
let newResolusi = Resolusi()
let newVideo = VideoMode()
// mengakses:
print(newResolusi.width, newResolusi.height, separator: "\n")
let variable = newVideo.resolution.height
print(variable)
// changing value
newVideo.resolution.height = 1920
newVideo.resolution.width = 1080
// memberwise
let vga = Resolusi(width: 768, height: 1366)
// menyalin nilai
let hd = Resolusi(width: 1080, height: 1920)
var cinema = hd
cinema.width = 2160
print("Menyalin nilai tidak akan mengubah nilai aslinya, lihat \(cinema.width) dan \(hd.width)")
let videoNow = VideoMode()
videoNow.resolution = vga
videoNow.interlaced = true
videoNow.frameRate = 26.0
videoNow.name = "High Definition"
if videoNow === newVideo {
print("Mereka berdua: videoNow dan newVideo sama!")
} else {
print("Mereka berdua tidak sama!")
}
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
class DataImporter {
var filename = "data.txt"
}
class DataManager {
lazy var importer = DataImporter()
var data = [String]()
}
let manager = DataManager()
manager.data.append("Some data")
manager.data.append("Some more data")
struct Point {
var x = 0.0
var y = 0.0
}
struct Shape {
var origin = Point(
var center: Point {
get {
return Point(x: origin.x/2, y: origin.y/2)
}
set(newCenter) {
origin.x = newCenter.x/2
origin.y = newCenter.y/2
}
}
}
struct Cuboid {
var width = 0.0, height = 0.0, depth = 0.0
var volume: Double {
return width * height * depth
}
}
let newVolume = Cuboid(width: 10.0, height:15.2, depth: 14.1)
print(newVolume.volume)
class StepCounter {
var totalSteps: Int = 0 {
willSet(newSteps) {
print("About to set totalSteps to \(newSteps)")
}
didSet {
if totalSteps > oldValue {
print("Added \(totalSteps - oldValue) steps")
}
}
}
}
// jika menggunakan static, maka untuk mengakses properti atau method harus dengan menggunakan jenisnya sendiri
// tidak bisa dengan menggunakan instance objek
struct SomeStructure {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 1
}
}
enum SomeEnumeration {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 6
}
}
class SomeClass {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 27
}
}
print(SomeClass.storedTypeProperty) // ingat tidak bisa diinisialiasi pada suatu instance
class Counter {
var count = 0
func increment() {
count += 1
}
func incrementBy(Angka: Int) {
count += Angka
}
func decrement() {
if count > 0 {
count -= 1
} else {
count = 0
}
}
func decrementBy(Angka: Int) {
if count > 0 {
count -= Angka
} else {
count = 0
}
}
func reset() {
count = 0
}
}
subscript(index: Int) -> Int {
get {
/* some expression put here */
}
set(newValue) {
/* take some actions */
}
}
struct TimesTable {
let multiplier: Int
subscript(index: Int) -> Int {
return multiplier * index
}
}
let three = TimesTable(multiplier: 3)
print(three[6]) // akan print (3 * 6)
struct Matriks {
let rows: Int, columns: Int
var grid: [Double]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(repeating: 0.0, count: rows * columns)
}
func IndexIsValid(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> Double {
get {
assert(IndexIsValid(row: row, column: column), "Index out of range")
return grid[(row * columns) + column]
}
set {
assert(IndexIsValid(row: row, column: column), "Index out of range")
grid[(row * columns) + column] = newValue
}
}
}
// type subscripts
enum Planet: Int {
case merkurius = 1, venus, bumi, mars, jupiter, saturnus, uranus, neptunus
static subscript(n: Int) -> Planet {
return Planet(rawValue: n)!
}
}
let mars = Planet[4]
print(mars)
class Vehicle {
var currentSpeed = 0.0
var description: String {
return "Traveling at \(currentSpeed) MpH"
}
func makeNoise() {
// do nothing - just an arbitrary function
}
}
// class inheritance with: ": "
class Bicycle: Vehicle {
var hasBasket = false
}
let bicycle = Bicycle()
print(bicycle.hasBasket, bicycle.currentSpeed, separator: "\t")
// nested class
class Tandem: Bicycle {
var numberOfPassengers = 2
}
let tandem = Tandem()
print(tandem.hasBasket)
// override function or something
class Train: Vehicle {
override func makeNoise() {
print("Choo Choo")
}
}
class Car: Vehicle {
var gear = 1
override var description: String {
return super.description + " in gear \(gear)"
}
} // mengakses var description dari vehicle menggunakan super.description
// gunakan final di awal var, function, property jika ingin menetapkannya tidak bisa diubah
// kala inheritance