Created
December 17, 2016 14:20
-
-
Save takoikatakotako/1753a661cc62fefdae6e4e6d88bcd7cc to your computer and use it in GitHub Desktop.
辞書配列を内包するArrayの抽出サンプル。 Ob-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
| import UIKit | |
| class ViewController: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // 入れ子にする辞書配列を用意します。 | |
| let person0: Dictionary<String, String> = ["id": "0", "name": "kabigon", "age": "24"] | |
| let person1: Dictionary<String, String> = ["id": "1", "name": "yadon", "age": "23"] | |
| let person2: Dictionary<String, String> = ["id": "2", "name": "pikachu", "age": "21"] | |
| //用意した辞書配列を配列に入れていきます。 | |
| var personsAry: Array<Any> = [] | |
| personsAry.append(person0) | |
| personsAry.append(person1) | |
| personsAry.append(person2) | |
| // idが1番の人のdicを取り出す | |
| let getDic: Dictionary<String, String> = getDicInAey(dictonarysAry: personsAry, key: "id", value: "1") | |
| print(getDic) | |
| } | |
| func getDicInAey(dictonarysAry:Array<Any>, key: String, value: String) -> Dictionary<String, String> { | |
| //keyの値に対応するものを取り出し | |
| for tempDic in dictonarysAry{ | |
| var dic = tempDic as! Dictionary<String, String> | |
| if dic[key] == value{ | |
| return dic | |
| } | |
| } | |
| //もし指定のキーと値が無かったら空の辞書配列を返す | |
| let returnDic:Dictionary<String, String> = [:] | |
| return returnDic | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment