This file contains hidden or 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
// エラーハンドリング専用構文 | |
do { | |
// エラーが発生する可能性があるコード | |
} catch { | |
// エラー発生時の処理 | |
} | |
// 例 | |
do { |
This file contains hidden or 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
if case let .OSX(version) = platform { | |
print(version) | |
} | |
if case let value? = optionalValue { | |
} | |
if case let (a?, b?, c?) = (optionalA, optionalB, optionalC) { | |
// optionalA,B,Cすべてがoptionalじゃないときだけ実行される | |
print(\(a)\(b)\(c)) |
This file contains hidden or 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
Realm().write { | |
Realm().deleteAll() | |
} | |
let turtle = Animal() | |
turtle.name = "Turtle" | |
turtle.legCount = 4 | |
Realm().write { | |
Realm().add(turtle) |
This file contains hidden or 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
alias imgdiff='composite -compose difference' |
This file contains hidden or 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
class HTMLElement { | |
let name: String | |
let text: String? | |
// プロパティでasHTMLへのstrong参照を持っている | |
lazy var asHTML: Void -> String = { // nameとtextを使いたいからlazy | |
// Closure内でselfのプロパティをキャプチャしている | |
if let text = self.text { | |
return "<\(self.name)>\(text)</\(self.name)>" |
This file contains hidden or 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 sum: ([Int]) -> Int | |
sum = { values in | |
if values.isEmpty { | |
return 0 | |
} | |
else { | |
return values.first! + sum(Array(values.dropFirst())) | |
} | |
} |
This file contains hidden or 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 Location { | |
var x:Int | |
var y:Int | |
} | |
var l1 = Location(x: 10, y: 20) | |
l1.x = 30 | |
let l2 = Location(x: 10, y: 20) | |
l2.x = 30 // Cannot assign to property |
This file contains hidden or 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
public override static func initialize() { | |
struct Static { | |
static var token: dispatch_once_t = 0 | |
} | |
struct SwizzlingSelector { | |
let original:Selector | |
let swizzled:Selector | |
} | |
This file contains hidden or 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
protocol P: Hashable {} | |
func ==(lhs: S, rhs: S) -> Bool { return lhs.name == rhs.name } | |
struct S: P { | |
var name = "struct" | |
var hashValue: Int { get{ return name.hash } } | |
} | |
let setS: Set<S> = [S()] | |
let setP: Set<P> = [S()] // Protocol 'P' can only be used as a generic constraint because it has Self or assosiated type requirements |
This file contains hidden or 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
protocol P1 { | |
} | |
protocol P2 { | |
typealias A = Self | |
} | |
let arr1: Array<P1> = [] | |
let arr2: Array<P2> = [] // Protocol 'P2' can only be used as a generic constraint because it has Self or associated type requirements |