zdogma / aikatsulyric Goto Github PK
View Code? Open in Web Editor NEWアイカツ歌詞と関連動画を閲覧できる iOS アプリ
アイカツ歌詞と関連動画を閲覧できる iOS アプリ
Caution の数が多そうであれば種類別に PR を分けて対応する。
#1 (comment)
特に
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 {
// データがないとき
}
のどちらかといった感じです。
お作法的なところ
SwiftLintに従って書くと大体コミュニティーのベストプラクティスに則れます。
https://github.com/realm/SwiftLint
Cocoapodsで導入してXcodeのBuildPhaseでチェックするようにするといいです。
持ち回すデータの型(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
}
例外の作法
なんつっても例外を握りつぶさないってとこですね。
こちらも参照してみてください。
https://ez-net.jp/article/B2/cwtBwURB/pp3ZiIOQz2Cc/
Result型でのError表現などにも触れられています。
Result型使いたいときは
https://github.com/antitypical/Result
これが良いです。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.