Skip to content

Instantly share code, notes, and snippets.

@DJNgoma
Created June 13, 2016 06:42
Show Gist options
  • Save DJNgoma/937ad0891d2be431a3734762db73296b to your computer and use it in GitHub Desktop.
Save DJNgoma/937ad0891d2be431a3734762db73296b to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import UIKit
// Seems I missed quite the discussion. By default setters and getters are provided on all variables in Swift with the use of either let or var respectively.
// So if on basic types (structs) I want:
// - getters only, I use let.
// - getters/setters, I use var.
// An example would be the following using Int structs:
let a: Int = 0 // Read
var b: Int = a // Read/Write
//a = 1 // Complication error, can only get
b = 2 // Compiles, due to var
print(a) // Gets 1
print(b) // Get New Value of 2
// I use **private**, pretty much encapsulation. The use of **public** would be usually for frameworks. **internal** is the default, so usually you would use this for instance methods/properties.
// The use the keywords get & set in variables would be computed values.
let c: Int = 0
var d: Int { get { return a } } // Pretty much now a let LOL, if a let value is used for the get return.
var e: Int { get { return b } set { } }
//d = 1 // Compilation error, only has get-only property
e = 1 // Compiles as it has set property.
// A bigger problem would be the use of reference/value types and copying isn't done. i.e. classes/structs.
class SomeClass:NSCopying {
var someValue: Int
init() { self.someValue = 0 }
init(someValue: Int) { self.someValue = someValue }
@objc func copyWithZone(zone: NSZone) -> AnyObject {
return SomeClass()
}
}
let f: SomeClass = SomeClass()
var g: SomeClass = f
var h: SomeClass = f.copyWithZone(nil) as! SomeClass
f.someValue = 1
g.someValue = 2
h.someValue = 3
print(f.someValue) // Get 2
print(g.someValue) // Get 2
print(h.someValue) // Get 3
// Final Class
final class FinalClass {
}
// Normal Struct
struct NormalStrut {
// final var structVariable: [String] // Compile error, structs can't have final
}
// Final is used only in classes or class variables.
class NormalClass {
// Class variables
var classVariable: Int
let classConstant: Int
// Final variables, these can't be overridden
final var finalClassVariable: String
final let finalClassConstant: String
// Static/Class variables (which are already final)
static var staticClassVariable: Double = 0.0
static let staticClassConstant: Double = 1.1
init() {
self.classVariable = 0
self.classConstant = 1
self.finalClassVariable = "a"
self.finalClassConstant = "b"
}
init(classVariable: Int, classConstant: Int, finalVariable: String, finalConstant: String) {
self.classVariable = classVariable
self.classConstant = classConstant
self.finalClassVariable = finalVariable
self.finalClassConstant = finalConstant
}
// Final Example Methods
func classMethod() -> String { return "classMethod" }
final func finalMethod() -> String { return "finalMethod" }
static func staticMethod() -> String { return "staticMethod" }
}
class SomeConcreteClass: NormalClass {
override init() {
super.init()
}
// Overriden variables have to be computed values, like Extensions
override var classVariable: Int { get { return 2 } set {} }
// override let classConstant: Int // Compile error as a constant can't be overwritten
// Nothing new to see with methods, just use the keyword override on the method
override func classMethod() -> String { return "overridden classMethod" }
// // Compile error, won't be able to override as variables are marked final
// override var finalVariable: String { get { return "" } set {} }
// override let finalConstant: String { get { return "" } set {} }
}
//// Compile error, won't be able to subclass as class is marked final
//class AnotherConcreteClass: SomeFinalClass {
//
//}
let i = NormalClass()
let j = SomeConcreteClass()
print(i.classVariable) // 0
print(i.classConstant) // 1
print(i.finalClassVariable) // "a"
print(i.finalClassConstant) // "b"
print(j.classVariable) // 2
print(j.classConstant) // 1
print(j.finalClassVariable) // "a"
print(j.finalClassConstant) // "b"
//print(i.staticClassVariable) // Compile error as variable is class and not instance type
//print(i.staticClassConstant) // Compile error as variable is class and not instance type
print(SomeConcreteClass.staticClassVariable) // 0.0
print(SomeConcreteClass.staticClassConstant) // 1.1
i.classVariable = 3
//i.classConstant = 4 // Compile error as variables is constant
i.finalClassVariable = "c"
//i.finalClassConstant = "d" // Compile error as variable is constant
SomeConcreteClass.staticClassVariable = 2.2
//SomeConcreteClass.staticClassConstant = 3.3 // Compile error as variable is constant
print(i.classVariable) // 3
print(i.classConstant) // 1
print(i.finalClassVariable) // "c"
print(i.finalClassConstant) // "b"
print(SomeConcreteClass.staticClassVariable) // 2.2
print(SomeConcreteClass.staticClassConstant) // 1.1
print(i.classMethod()) // classMethod
print(i.finalMethod()) // finalMethod
print(j.classMethod()) // overriden classMethod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment