reduce() vs. reduce(into:)
`reduce(_:_:)``reduce(into:_:)`
ๅๆธไธๅ๏ผ
reduce(into:_:)
ๆ inout parameter๏ผreduce(_:_:)
ๆฒๆ๏ผๆไปฅ็ถๅฎๅฐไนๅ็ดฏ็ฉ็็ตๆๅณๅ ฅ nextPartialResult ไธฆ้่ฆๆดๆฐๆฐ็็ดฏ็ฉ็ตๆๆ๏ผๅฐฑๅฟ ้ ่ค่ฃฝ (copy-on-write) ไนๅ็็ตๆ๏ผๆไปฅๅฆๆ Result ๆฏไธๅๅคงๅ็ Array ๆ Dictionary ไน้ก็ๅๅฅ๏ผๅฐฑๆๅฐ็จๅผ็ๆ็็ข็่กๆใๅๅณๆนๅผไธๅ๏ผ
reduce(_:_:)
ๆฏๅฐ nextPartialResult ๆๅพ็ return value ไฝ็บๆดๅๆนๆณ็ๅๅณๅผใreduce(into:_:)
ๅๆฏ็จ updateAccumulatingResult ๆ็บๅฐ partialResult ๅๆดๆฐ๏ผ็ถๅพไปฅ partialResult ๆๅพ็ๅผไฝ็บๅๅณๅผใ
ๆไปฅไฝฟ็จreduce(into:_:)
ๆ่ฆๆณจๆ๏ผใไธๅฎ่ฆ้้ updateAccumulatingResult ไพๆดๆฐ partialResultใ๏ผๅฆๅๅๅณๅผๆๆฏๅไพ็ initialResultโ
.reduce(into:) is preferred over reduce() for efficiency when the result is a copy-on-write type, for example an Array or a Dictionary. ๐ .reduce(into:_:)
Example
View โฉ Graphics and Rendering โฉ
ๆๅ็จๅไฝต SwiftUI ไธญ็ transitions ไพ่ไพ๏ผ่ชชๆ้ๅ ฉ่ ็็จๆณๆๅชไบไธๅใๅจๅไฝต SwiftUI ไธญ็ transitions ๆ๏ผไฝฟ็จ SwiftUI ๅๆ็่ชๆณ๏ผ้กฏๅพๅคช้ๅ้ท๏ผ
่ฅไฝฟ็จ .reduce() ๆ .reduce(into:)๏ผๅๅฏไปฅๅฐ่ชๆณ่ฎ็บๆด็ฒพ็ฐก๏ผ
ๅๆๆฏ่ฆๅ ๅ ๅ ฅไปฅไธ็ extension๏ผ
่ณๆผ็จ .reduce() ้ๆฏ .reduce(into:)๏ผๅฎๅไน้ๆไฝไธๅ๏ผ่ซ็ไธ้ข็็จๅผ็ขผ๏ผ
.reduce()
.reduce(into:)
Last updated