LoginSignup
1

More than 1 year has passed since last update.

ID を持つエンティティにはIdentifiableを積極的に採用

Posted at

before

struct User: Decodable { 
  let id: Int
}

struct UserDataSource {
  // 引数はIntであればなんでも受け入れるように見えてしまう。 
  func loadUser(for id: Int) -> User {
  // 本来はDBなどから取得したものを返すなど
   } 
}
let user = UserDataSource().loadUser(for: 1)

after

struct User: Decodable , Identifiable { 
  let id: Int
}

struct UserDataSource {
   func loadUser(for id: User.ID) -> User {
          // DBなどから取得したものを返すなど
   } 
}
 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1