Last active
August 29, 2015 14:02
-
-
Save YusukeHosonuma/263c01f350e9ba9bf83e to your computer and use it in GitHub Desktop.
Swift個人メモ ref: http://qiita.com/YusukeHosonuma/items/1d82bd2527cb7705759d
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 old = 17 | |
let foo = "I am \(old) old years." |
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 foo = "I am \(old) old years." |
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
var array = ["apple", "orange", "banana"] |
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
var array = ["apple", "orange", "banana"]; |
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
// 変数:型 で引数指定 | |
// -> で関数の戻り値の型を指定 | |
// (だいたいScalaの構文をそのまま持ち込んでる印象) | |
func greet(firstName: String, lastName: String) -> String { | |
return "My name is \(firstName) \(lastName)." | |
} | |
greet("Yamada", "Taro") |
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
// 変数:型 で引数指定 | |
// -> で関数の戻り値の型を指定 | |
// (だいたいScalaの構文をそのまま持ち込んでる印象) | |
func greet(firstName: String, lastName: String) -> String { | |
return "My name is \(firstName) \(lastName)." | |
} | |
greet("Yamada", "Taro") |
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
func foo() -> (Int, Int, Int) { | |
} | |
// Scalaと違って配列で返すので数の制限は無し? |
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
func foo() -> (Int, Int, Int) { | |
} | |
// Scalaと違って配列で返すので数の制限は無し? |
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
for sum(numbers: Int...) -> Int { | |
} |
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
for sum(numbers: Int...) -> Int { | |
} |
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
func foo() { | |
func bar() { | |
} | |
bar() | |
} |
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
func foo() { | |
func bar() { | |
} | |
} |
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
// conditionに「Intを引数にBoolを返す関数」を受け取る | |
func numberFilter(numbers: Int[], condition: Int -> Bool) -> Int[] { | |
} | |
// 「Int型を引数に取り、Int型を返す関数」を返す関数 | |
func foo() -> (Int -> Int) { | |
} |
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
// conditionに「Intを引数にBoolを返す関数」を受け取る | |
func numberFilter(numbers: Int[], condition: Int -> Bool) -> Int[] { | |
} | |
// 「Int型を引数に取り、Int型を返す関数」を返す関数 | |
func foo() -> (Int -> Int) { | |
} |
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
// 数値配列numbersの値を全て2倍にする | |
numbers.map({ | |
(number: Int) -> Int in | |
let result = number * 2 | |
return result | |
}) | |
// 省略するとここまで短く書ける | |
numbers.map({ number in number * 2 }) | |
// ちなみにこんなことも(シンタックスシュガーでしょうが) | |
sort([1, 5, 3, 12, 2]) { $0 > $1 } |
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
// 数値配列numbersの値を全て2倍にする | |
numbers.map({ | |
(number: Int) -> Int in | |
let result = number * 2 | |
return result | |
}) | |
// 省略するとここまで短く書ける | |
numbers.map({ number in number * 2 }) | |
// ちなみにこんなことも(シンタックスシュガーでしょうが) | |
sort([1, 5, 3, 12, 2]) { $0 > $1 } |
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 Person { | |
// インスタンス変数 | |
// 全てのインスタンス変数は宣言またはinitで初期化する必要がある | |
var name: String | |
var age: Int = 0 | |
// コンストラクタ | |
init(name: String) { | |
self.name = name | |
} | |
// メソッド | |
func greet() -> String { | |
} | |
// deinitを宣言するとデストラクタとして機能する | |
} |
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 Person { | |
// インスタンス変数 | |
// 全てのインスタンス変数は宣言またはinitで初期化する必要がある(コンパイラ要件?) | |
var name: String | |
var age: Int = 0 | |
// コンストラクタ | |
init(name: String) { | |
self.name = name | |
} | |
// メソッド | |
func greet() -> String { | |
} | |
// deinitを宣言するとデストラクタとして機能する | |
} |
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
var person = Person() // newキーワードなし(ScalaのTypeClassみたい) |
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
var person = Person() // newキーワードなし(ScalaのTypeClassみたい) |
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
override func foo() -> String { | |
} |
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
override func foo() -> String { | |
} |
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 Person: Animal { | |
} |
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 Person: Animal { | |
} |
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
var names = [ | |
"Yamada": "Taro", | |
"Tanaka": "Jirou", | |
] |
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
var names = [ | |
"Yamada": "Taro", | |
"Tanaka": "Jirou", | |
] |
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
var foo: Int { | |
get { | |
return bar * 2 // 意味のあるコードではありません | |
} | |
set { | |
foo = newValue / 2 // setの値は`newValue`に格納される | |
} |
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
var foo: Int { | |
get { | |
return foo * 2 | |
} | |
set { | |
foo = newValue / 2 // setの値は`newValue`に格納される | |
} |
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
// 値の取得・設定をカスタマイズしたくはないけど監視だけしたい場合は以下のようにする | |
// CocoaTouchで採用されているKVOを実現するための機構? | |
var foo: Int { | |
willSet { | |
} | |
} |
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
// 値の取得・設定をカスタマイズしたくはないけど監視だけしたい場合は以下のようにする | |
// CocoaTouchで採用されているKVOを実現するための機構? | |
var foo: Int { | |
willSet { | |
} | |
} |
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 Person { | |
// 第2引数はシグネチャとしては`lastName`、内部変数としては`ln`が採用される | |
// (これにより無駄にコードが長くなることがなくなりそう) | |
func greet(firstName: String, lastName ln: String) -> String { | |
return "My name is \(firstName) \(ln)."¥ | |
} | |
} | |
// 呼び出し | |
ver yamada = Person() | |
yamada.greet("Yamada", lastName: "Taro") // この辺りはObjective-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
// 宣言 | |
class Person { | |
// 第2引数はシグネチャとしては`lastName`、内部変数としては`ln`が採用される | |
// (これにより無駄にコードが長くなることがなくなりそう) | |
func greet(firstName: String, lastName ln: String) -> String { | |
return "My name is \(firstName) \(ln)."¥ | |
} | |
} | |
// 呼び出し | |
ver yamada = Person() | |
yamada.greet("Yamada", lastName: "Taro") // この辺りはObjective-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
// このあたりHaskellのLeft|Rightを思い出す | |
enum Result { | |
case Success(String, String) | |
case Error(String) | |
} |
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
// このあたりHaskellのLeft|Rightを思い出す | |
enum Result { | |
case Success(String, String) | |
case Error(String) | |
} |
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 ExampleProtocol { | |
var foo: String { get } // 必須のreadプロパティ | |
mutating func bar() // ミュータブルな操作としてのマーク?(よく分からん) | |
} |
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 ExampleProtocol { | |
var foo: String { get } // 必須のreadプロパティ | |
mutating func bar() // ミュータブルな操作としてのマーク?(よく分からん) | |
} |
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 Person: ExampleProtocol { | |
} |
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 Person: ExampleProtocol { | |
} |
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
// 旧カテゴリ拡張だけどもScalaを彷彿とさせる・・・ | |
// (以下だいたい本からの丸写し) | |
extension Int: ExampleProtocol { | |
var simpleDescription: String { | |
return "I am a number of \(self)." | |
} | |
mutating func adjust() { | |
self += 42 | |
} | |
} | |
7.simpleDescription |
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
// 旧カテゴリ拡張だけどもScalaを彷彿とさせる・・・ | |
// (以下だいたい本からの丸写し) | |
extension Int: ExampleProtocol { | |
var simpleDescription: String { | |
return "I am a number of \(self)." | |
} | |
mutating func adjust() { | |
self += 42 | |
} | |
} | |
7.simpleDescription |
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
// あえてJava風にTとか書いてみる | |
func repeat<T>(item: T, times: Int) -> T[] { | |
} |
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
// あえてJava風にTとか書いてみる | |
func repeat<T>(item: T, times: Int) -> T[] { | |
} |
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
// だいたいJavaと同じ | |
let emptyArray = String[]() | |
let emptyDictionary = Dictionary<String, Float>() |
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
// だいたいJavaと同じ | |
let emptyArray = String[]() | |
let emptyDictionary = Dictionary<String, Float>() |
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
for player in players { | |
} |
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
for player in players { | |
} |
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 old < 20 { | |
} else { | |
} | |
// 条件式全体を()で囲む必要がない! |
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 old < 20 { | |
} else { | |
} | |
// 条件式全体を()で囲む必要がない! |
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
var optionalString: String? = "Hello" | |
if let foo = optionalString { | |
// ここではfooがnilでないことが保証される? | |
} | |
// ScalaのOption型と似た考え方。 |
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
var optionalString: String? = "Hello" | |
if let foo = optionalString { | |
// ここではfooがnilでないことが保証される? | |
} | |
// ScalaのOption型を構文に組み込んだ印象を受けるが(英語力が不足してわからない) |
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 filename = "foo.txt" | |
switch filename { | |
case: "bar.txt": | |
// 明示的なbreakは不要 | |
case: let f where f.hasSuffix(".txt") | |
// let where による条件判定 | |
default: | |
// default | |
} |
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 filename = "foo.txt" | |
switch filename { | |
case: "bar.txt": | |
// 明示的なbreakは不要? | |
case: let f where f.hasSuffix(".txt") | |
// let where による条件判定 | |
default: | |
// default | |
} |
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
// 型を明示しないで良い | |
for (name, phoneNumbers) in addresses { | |
} |
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
// 型を明示しないで良い | |
for (name, phoneNumbers) in addresses { | |
} |
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
for i in 0..3 { | |
} |
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
for i in 0..3 { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment