Skip to content
New issue

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

画像読み込みのために Kingfisher を導入 #1

Merged
merged 2 commits into from
Feb 21, 2017

Conversation

zdogma
Copy link
Owner

@zdogma zdogma commented Feb 20, 2017

背景

UIImageViewに画像取ってきて突っ込むのは

https://github.com/onevcat/Kingfisher
github.com
GitHub - onevcat/Kingfisher: A lightweight, pure-Swift library for downloading and caching images from the web.
Kingfisher - A lightweight, pure-Swift library for downloading and caching images from the web.

これ使うと楽ですよ。

TODO リスト

@keitaoouchi
Copy link
Collaborator

このPRに関してはこれでいい感じですねー。いくつか元のコードベースについてコメントします。

お作法的なところ

SwiftLintに従って書くと大体コミュニティーのベストプラクティスに則れます。
https://github.com/realm/SwiftLint
Cocoapodsで導入してXcodeのBuildPhaseでチェックするようにするといいです。

特に

  • force_cast
  • force_try
  • force_unwrapping

をなるべく避けるようにするとよりSwiftらしくなると思います。

例えばこの辺のforce unwrappingを治すとしたら、

  • var dataを暗黙的アンラップ型で定義して、dataに値が渡ってなかったら非正常系というのを型で表現し、try ~ catchで例外を補足して適当にエラー表示する
  • Optional Chaining、Optional Bindingを使って、dataにデータが渡されてるとき(正常系)と、渡されてないとき(非正常系)を表示分けする
if let thumbnailUlr = data?.thumbnailUrl, !thumbNailUrl.isEmpty {
  // データがあるとき
} else {
  // データがないとき
}

のどちらかといった感じです。

持ち回すデータの型(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
}

Swiftの文法

某オフィスにたくさん並んでいる詳解Swiftもいいですが、なんだかんだでAppleの公式が常に最新キャッチアップでSwiftらしさもしっかり解説されててオススメです。

@zdogma
Copy link
Owner Author

zdogma commented Feb 21, 2017

@keitaoouchi
こちらインプットいただきありがとうございます!勉強になります!
いただいたものは Issue 化して、別途修正 PR を出します! 🐰

@zdogma
Copy link
Owner Author

zdogma commented Feb 21, 2017

いただいたコメントの内容は Issue 化したので、一旦こちらの PR はマージします!
マージ

@zdogma zdogma merged commit 76750b2 into master Feb 21, 2017
@zdogma zdogma deleted the devel/introduce_Kingfisher branch February 21, 2017 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants