実現したいこと
下記コード①の様にdictionaryを作り、struct aaaの要素全てを取得することはできますが、
別の方法として、その中のuserNameのみ取得する方法を知りたい。
やったこと
struct aaaと同じ様にuserNameのみのstruct bbbを作りましたが、
struct aaaのuserNameと別物のuserNameになってしまう為、
struct aaaのuserNameを使い回したいです。
要素全てを取得する方法は下記コード②にあります。
この書き方に近い方法で、userNameのみ取得する方法を教えていただきたいです。
よろしくお願いします。
コード① import Foundation protocol Document { init?(dictionary:[String:Any]) } struct aaa { var userName :String var hoge :String var hogehoge: String var huga: String var dictionary:[String:Any] { return ["userName" : userName, "hoge" : hoge, "hogehoge" : hogehoge, "huga" : huga ]} } extension aaa : Document { init?(dictionary: [String:Any]) { guard let userName = dictionary["userName"] as? String else {return nil} guard let hoge = dictionary["hoge"] as? String else {return nil} guard let hogehoge = dictionary["hogehoge"] as? String else {return nil} guard let huga = dictionary["huga"] as? String else {return nil} self.init(userName: userName as String, hoge: hoge as String, hogehoge: hogehoge as String, huga: huga as String) } }
コード② ...省略 let aaa1 = aaa(userName: userNameField.text!, hoge: hoge.text!, hogehoge: hogehoge.text!, huga: huga.text!) db.collection().document().setData(aaa1.dictionary)... ...省略
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/07/17 00:02