Open onmyway133 opened 3 years ago
Sometimes we need to update some properties between objects, for example
book.name = updatedBook.name book.page = updatedBook.page book.publishedAt = updatedBook.publishedAt
Repeating the caller book is tedious and error-prone. In Kotlin, there is with block which is handy to access the receiver properties and methods without referring to it.
book
with(book) { name = updatedBook.name page = updatedBook.page publishedAt = updatedBook.publishedAt }
In Swift, there are no such thing, we can write some extension like
extension Book { func update(with anotherBook: Book) { name = anotherBook.name page = anotherBook.page publishedAt = anotherBook.publishedAt } }
Or simply, we can just use forEach with just that book
forEach
[book].forEach { $0.name = updatedBook.name $0.page = updatedBook.page $0.publishedAt = updatedBook.publishedAt }
Sometimes we need to update some properties between objects, for example
Repeating the caller
book
is tedious and error-prone. In Kotlin, there is with block which is handy to access the receiver properties and methods without referring to it.In Swift, there are no such thing, we can write some extension like
Or simply, we can just use
forEach
with just that book