Last active
June 17, 2016 09:09
-
-
Save gitbricho/82b34135bf1f94110971 to your computer and use it in GitHub Desktop.
swift:collection
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
import UIKit | |
//## 配列の定義 | |
//# 空の配列の作成 | |
var v整数配列 = [Int]() //[] | |
print("v整数配列の要素数:\(v整数配列.count)") | |
//結果:v整数配列の要素数:0\n | |
//# 配列に値を追加 | |
v整数配列.append(5) //[5] | |
//# 配列を空にする | |
v整数配列 = [] //[] | |
//# デフォルト値を持つ配列の作成 | |
// (要素数と繰り返しの値で設定) | |
//(--- Swift3 ---) update:16/06/15 | |
//var v2個の文字列配列 = [String](count: 2, repeatedValue: "ABC") //["ABC", "ABC"] | |
var v2個の文字列配列 = [String](repeating: "ABC", count: 2) //["ABC", "ABC"] | |
// (配列定数で設定) | |
var v3個の文字列配列1: [String] = ["XXX", "YYY", "ZZZ"] | |
var v加算配列 = v2個の文字列配列 + v3個の文字列配列1 //["ABC", "ABC", "XXX", "YYY", "ZZZ"] | |
// (型推論):要素の型から [String]を推論 | |
var v3個の文字列配列2 = ["XX", "YY", "ZZ"] |
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
import Foundation | |
//## 配列のメソッド | |
//# count: 要素数を返す | |
var v配列2 = ["ABC", "ABC", "XXX", "YYY", "ZZZ"] | |
print("v配列2の要素数:\(v配列2.count)") | |
//結果:v配列2の要素数:5\n | |
//# isEmpty: 配列が空か調べる | |
if v配列2.isEmpty { | |
print("v配列2は空") | |
} else { | |
print("v配列2は空ではない") | |
} | |
//結果: v配列2は空ではない\n |
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
import Foundation | |
//## 配列の操作 | |
var v配列a = ["要素1", "要素2"]; | |
//# 要素の追加: append メソッド | |
v配列a.append("新要素") //["要素1", "要素2", "新要素"] | |
//# 要素の追加: += | |
v配列a += ["追加"] //["要素1", "要素2", "新要素", "追加"] | |
//# 先頭の要素:"要素1" の取得 | |
var v先頭要素 = v配列a[0] //"要素1" | |
//# 末尾の要素:"追加" の取得 | |
var v最終要素 = v配列a[v配列a.count-1] //"追加" | |
//# 要素から index を取得 | |
//(--- Swift3 ---) 更新16/06/15 | |
//let index = ["aa", "bb", "cc"].indexOf("bb") //1 | |
let index = ["aa", "bb", "cc"].index(of: "bb") //1 | |
//# 要素[1]:"要素2" の変更 | |
v配列a[1] = "変更" | |
print(v配列a) //結果:["要素1", "変更", "新要素", "追加"] | |
//# 指定範囲[1-2]の要素の変更 | |
v配列a[1...2] = ["変1", "変2"] | |
print(v配列a) //結果:["要素1", "変1", "変2", "追加"] | |
//# 指定範囲[1-末尾]の要素の変更 | |
v配列a[1...v配列a.count-1] = ["変x", "変y"] | |
print(v配列a) //結果:["要素1", "変x", "変y"] | |
//# 要素の挿入 | |
//(--- Swift3 ---) 更新16/06/15 | |
//v配列a.insert("挿入", atIndex: 1) //["要素1", "挿入", "変x", "変y"] | |
v配列a.insert("挿入", at: 1) //["要素1", "挿入", "変x", "変y"] | |
//# 要素の削除 | |
//(--- Swift3 ---) 更新16/06/15 | |
//v配列a.removeAtIndex(0) //["挿入", "変x", "変y"] | |
v配列a.remove(at: 0) | |
print(v配列a) //結果: ["挿入", "変x", "変y"] | |
//# 全要素の削除 | |
v配列a.removeAll() //[] | |
//# 文字列配列から文字列を生成 | |
//(--- Swift3 ---) 更新16/06/15 | |
//let l結合文字列 = ["aa", "bb", "cc"].joinWithSeparator(" ") //"aa bb cc" | |
let l結合文字列 = ["aa", "bb", "cc"].joined(separator: " ") //"aa bb cc" |
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
import Foundation | |
//## 様々な要素の配列 | |
let l異なる型の要素の配列 : [Any] = ["文字列", 12, 5.22] | |
//# is を使って要素の型を判定 | |
for 要素 in l異なる型の要素の配列 { | |
if 要素 is Int { | |
print("\(要素) は整数") | |
} else if 要素 is String { | |
print("\(要素) は文字列") | |
} else if 要素 is Double { | |
print("\(要素) は Double") | |
} | |
} | |
/* 結果: | |
文字列 は文字列 | |
12 は整数 | |
5.22 は Double | |
*/ | |
//# 関数の配列 | |
var v文字列を返す関数の配列: [(String) -> String] = [] | |
// update: 16/06/15 | |
func f文字列を返す関数を作成(_ str1: String) -> (String)->String { | |
func f作成関数(str2: String) -> String { | |
return str1 + ":" + str2 | |
} | |
return f作成関数 | |
} | |
v文字列を返す関数の配列.append(f文字列を返す関数を作成("xxx")) | |
v文字列を返す関数の配列.append(f文字列を返す関数を作成("yyy")) | |
v文字列を返す関数の配列[0]("aaa") //xxx:aaa | |
v文字列を返す関数の配列[0]("bbb") //xxx:bbb | |
v文字列を返す関数の配列[1]("ccc") //yyy:ccc | |
//# ストラクチャの配列 | |
struct S住所 { | |
var 郵便番号: String = "", 都道府県: String = "", 市区町村: String = "" | |
} | |
let l住所一覧 = [ | |
S住所(郵便番号:"1112222", 都道府県: "東京都", 市区町村: "港区"), | |
S住所(郵便番号:"1113222", 都道府県: "神奈川県", 市区町村: "横浜市") | |
] |
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
import Foundation | |
//------------------------------ | |
//## セット(Sets) ##### | |
//## 文字 Character の Set を宣言 | |
var l文字セット = Set<Character>() | |
print("l文字セットは Set<Character> 型、項目数=\(l文字セット.count)") | |
// 結果: l文字セットは Set<Character> 型、項目数=0 | |
//## l文字セットに、Character 型の値を一つ挿入 | |
l文字セット.insert("a") | |
print(l文字セット) //結果: ["a"] | |
//## l文字セットを空にする | |
l文字セット = [] | |
print(l文字セット) //結果: [] |
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
import Foundation | |
//## Set が空かチェック | |
var l文字セット2 = Set<Character>() | |
if l文字セット2.isEmpty { | |
print("l文字セット2は空") | |
} | |
// 結果: l文字セット2は空\n |
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
import Foundation | |
//## 配列定数から作成 | |
var v文字列セット3a: Set<String> = ["文字列1", "文字列2"] | |
var v文字列セット3b: Set = ["文字列1", "文字列2"] | |
//# 要素の挿入 | |
v文字列セット3a.insert("文字列1a") | |
print(v文字列セット3a) //結果: ["文字列2", "文字列1a", "文字列1"] | |
// すでに存在するものは挿入できない | |
v文字列セット3a.insert("文字列1") | |
print(v文字列セット3a) //結果: ["文字列2", "文字列1a", "文字列1"] | |
v文字列セット3a.insert("文字列2a") | |
print(v文字列セット3a) //結果:["文字列1a", "文字列1", "文字列2", "文字列2a"] |
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
import Foundation | |
//## 削除 | |
var v文字列セット4: Set = ["文字列1", "文字列2"] | |
v文字列セット4.remove("文字列1") //文字列1 を返す | |
print(v文字列セット4) //["文字列2"]\ | |
// 存在しない場合は削除できない | |
v文字列セット4.remove("文字列1") //nil を返す | |
print(v文字列セット4) //["文字列2"]\ |
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
import Foundation | |
//## 指定要素が含まれるかチェック | |
var v文字列セット5: Set = ["文字列1", "文字列2"] | |
if v文字列セット5.contains("文字列2") { | |
print("文字列2は含まれる") | |
} | |
// 結果: 文字列2は含まれる |
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
import Foundation | |
//## ソートしてループ | |
var v文字列セット6: Set = ["orange", "banana", "apple", "grape"] | |
//(--- Swift3 ---) update: 16/06/15 | |
//for 文字列 in v文字列セット6.sort() { | |
for 文字列 in v文字列セット6.sorted() { | |
print(文字列) | |
} | |
/* 結果: | |
apple | |
banana | |
grape | |
orange | |
*/ |
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
import Foundation | |
//## 集合演算 | |
var lAの趣味: Set = ["読書", "ピアノ"] | |
var lBの趣味: Set = ["読書", "ギター"] | |
let l二人の趣味・和 = lAの趣味.union(lBの趣味) //{"ピアノ", "読書", "ギター"} | |
//(--- Swift3 ---) update 16/06/15 | |
//let l共通の趣味・積 = lAの趣味.intersect(lBの趣味) //{"読書"} | |
let l共通の趣味・積 = lAの趣味.intersection(lBの趣味) //{"読書"} | |
//(--- Swift3 ---) update 16/06/15 | |
//let lAだけの趣味・差 = lAの趣味.subtract(lBの趣味) //{"ピアノ"} | |
let lAだけの趣味・差 = lAの趣味.subtracting(lBの趣味) //{"ピアノ"} | |
//(--- Swift3 ---) update 16/06/15 | |
//let lBだけの趣味・差 = lBの趣味.subtract(lAの趣味) //{"ギター"} | |
let lBだけの趣味・差 = lBの趣味.subtracting(lAの趣味) //{"ギター"} | |
//(--- Swift3 ---) update 16/06/15 | |
//let l共通でない趣味・排他 = lAの趣味.exclusiveOr(lBの趣味) //{"ピアノ", "ギター"} | |
let l共通でない趣味・排他 = lAの趣味.symmetricDifference(lBの趣味) //{"ピアノ", "ギター"} |
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
import Foundation | |
//------------------------------ | |
//## 辞書(Dictionaries) ##### | |
// キー/値ペア | |
var v国の首都 = [String: String]() //空の辞書 | |
//## キー/値ペアの追加 | |
v国の首都["日本"] = "東京" //1個のキー/値ペア | |
v国の首都 = [:] //辞書を空にする | |
//## 定数で辞書を作成 | |
var v映画の監督 = ["ジョーズ":"スピルバーグ", "スターウォーズ":"ルーカス"] | |
//## 辞書のメソッド | |
//# count: ペアの数 | |
//# isEmpty: 空のチェック | |
//## 値の修正 | |
v映画の監督["スターウォーズ"] = "ジョージ・ルーカス" | |
print(v映画の監督) | |
//結果:["ジョーズ": "スピルバーグ", "スターウォーズ": "ジョージ・ルーカス"] | |
//## 繰り返し処理 | |
for (映画, 監督) in v映画の監督 { | |
print("\(映画): \(監督)") | |
} | |
/* 結果: | |
ジョーズ: スピルバーグ | |
スターウォーズ: ジョージ・ルーカス | |
*/ | |
for 映画 in v映画の監督.keys { | |
print("\(映画): \(v映画の監督[映画])") | |
} | |
/* 結果: | |
ジョーズ: Optional("スピルバーグ") | |
スターウォーズ: Optional("ジョージ・ルーカス") | |
*/ | |
for 監督 in v映画の監督.values { | |
print("監督名: \(監督)") | |
} | |
/* 結果: | |
監督名: スピルバーグ | |
監督名: ジョージ・ルーカス | |
*/ | |
//## 辞書からキーと値の配列を作成 | |
let l映画一覧 = [String](v映画の監督.keys) //["ジョーズ", "スターウォーズ"] | |
let l監督一覧 = [String](v映画の監督.values) //["スピルバーグ", "ジョージ・ルーカス"] | |
//## 値が配列の辞書 | |
let lロール = ["user1": ["管理者", "ユーザー"], "user2": ["ユーザー"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment