We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1 (comment)
持ち回すデータの型(Entity)を定義する ViewControllerのdataをSong型として定義してあげるとスッキリします。あとJSONマッピングをObjectMapperなどを使ってやるとJSONとEntity間の変換が見通しやすくなりますよ! import ObjectMapper struct Song: ImmutableMappable { let title: String let text: String let thumbnail_url: String let series: String let scene: String let singer: String let embed_movie_src: String init(map: Map) throws { title = try map.value("title") text = try map.value("text") thumbnail_url = try map.value("thumbnail_url") series = try map.value("series") scene = try map.value("scene") singer = try map.value("singer") embed_movie_src = try map.value("embed_movie_src") } } そんでSong型オブジェクトをSongListTableViewControllerとViewControllerで持ち回すようにするといいです。ローカルのJSONファイルから[Song]に変換するメソッドはたとえば次のようになります。 func loadSongs() throws -> [Song] { if let path = Bundle.main.path(forResource: "aikatsu_songs", ofType: "json"), let data = FileManager.default.contents(atPath: path) { let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! [[String: Any]] return try Mapper<Song>().mapArray(JSONArray: json) } else { // なにかがおかしいよエラー throw CustomError.somethingWrong } } // どっか別のファイルに定義してもよい enum CustomError: Error { case somethingWrong }
The text was updated successfully, but these errors were encountered:
Successfully merging a pull request may close this issue.
#1 (comment)
The text was updated successfully, but these errors were encountered: