Skip to content

Instantly share code, notes, and snippets.

View hanawat's full-sized avatar
👁️
eagle‐eyed

hanawat

👁️
eagle‐eyed
View GitHub Profile
@hanawat
hanawat / TypeInference.swift
Last active January 16, 2016 19:41
Defaults are given, the type is infered.
let foo = 0
print(foo.dynamicType) // Int
let bar = ""
print(bar.dynamicType) // String
@hanawat
hanawat / InheritanceArray.swif
Created January 16, 2016 17:59
Inheritance to Class (NSObject), it'll be the reference type.
import Foundation
let apple: NSMutableArray = ["🍎", "🍎"]
var orange = apple
orange.replaceObjectsInRange(NSMakeRange(0, apple.count), withObjectsFromArray: ["🍊", "🍊"])
print("apple=\(apple)", "orange=\(orange)") // a=[🍊, 🍊] b=[🍊, 🍊]
@hanawat
hanawat / inoutArgument.swift
Created January 16, 2016 18:58
Changing argument, inout is put on parameter.
func change<T>(inout target: T, value: T) {
target = value
}
var name = "Michel"
var age = 25
change(&name, value: "Bob")
change(&age, value: 5)
@hanawat
hanawat / WarnUsedResult.swift
Last active January 16, 2016 19:43
When not using return value, compiler issues a warning.
@warn_unused_result(message = "Use the result fuckathon")
func area(height height: Double, width: Double) -> Double {
return height * width
}
area(height: 2.0, width: 3.0) // Compile Error: Use the result fuckathon
@hanawat
hanawat / OverloadMethod.swift
Created January 16, 2016 20:06
Overload is possible using the same method name.
func user(name: String, hobby: String) {
print("My name is \(name). I like \(hobby).")
}
func user(name: String, hobby: String, greeting: String) {
print("My name is \(name). I like \(hobby). \(greeting).")
}
user("Bob", hobby: "Football")
user("Mary", hobby: "Cook", greeting: "Nice to meet you")
@hanawat
hanawat / NestAndAliasStruct.swift
Created January 17, 2016 15:43
Associated type: Struct can use a nest and an another name (Alias).
struct Date {
let year, month, day: Int
init(year: Int, month: Int, day: Int) {
self.year = year
self.month = month
self.day = day
}
}
@hanawat
hanawat / MutatingMethod.swift
Created January 17, 2016 16:07
Struct is the value type, but it's possible to change the property. *It's against a policy.
struct Date {
var year, month, day: Int
init(year: Int, month: Int, day: Int) {
self.year = year
self.month = month
self.day = day
}
mutating func increment(year year: UInt) {
@hanawat
hanawat / TypePropertyAndMethod.swift
Created January 19, 2016 17:03
Using Type property, a class, enum, and structure can make Value in itself.
struct Greet {
let string: String
static let words = ["Nice", "to", "meet", "see", "you."] // Type property
static var first = true // Type property
init(name: String) {
// Refer type property and method from initializer
string = Greet.first ? Greet.first(name) : Greet.usual()
@hanawat
hanawat / MutatingGetter.swift
Last active January 20, 2016 17:01
It's also possible to use mutating or a nonmutating modifier at getter and setter in the computed property.
struct Reference {
private let _value: Double
var count = 0
init(value: Double) { _value = value }
var value: Double { mutating get {
count += 1 // Mutating property
return _value
}}
@hanawat
hanawat / willAndDidSet.swift
Created January 21, 2016 23:05
Notice can be understood as the change property before or after.
struct Product {
let cost: Int
var profit = 0
var count = 0
init(cost: Int, price: Int) {
self.cost = cost
self.price = price
}