This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let foo = 0 | |
print(foo.dynamicType) // Int | |
let bar = "" | |
print(bar.dynamicType) // String |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let apple: NSMutableArray = ["🍎", "🍎"] | |
var orange = apple | |
orange.replaceObjectsInRange(NSMakeRange(0, apple.count), withObjectsFromArray: ["🍊", "🍊"]) | |
print("apple=\(apple)", "orange=\(orange)") // a=[🍊, 🍊] b=[🍊, 🍊] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func change<T>(inout target: T, value: T) { | |
target = value | |
} | |
var name = "Michel" | |
var age = 25 | |
change(&name, value: "Bob") | |
change(&age, value: 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Date { | |
let year, month, day: Int | |
init(year: Int, month: Int, day: Int) { | |
self.year = year | |
self.month = month | |
self.day = day | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Product { | |
let cost: Int | |
var profit = 0 | |
var count = 0 | |
init(cost: Int, price: Int) { | |
self.cost = cost | |
self.price = price | |
} |
OlderNewer