@_functionBuilder public struct DictionaryBuilder<Key: Hashable, Value> { public func buildBlock() -> [Key: Value] { [:] } public func buildBlock(_ c0: [Key: Value]) -> [Key: Value] { c0 } public func buildBlock(_ c0: [Key: Value], _ c1: [Key: Value]) -> [Key: Value] { c0.merging(c1) { _, new in new } } } extension Dictionary { public init(@DictionaryBuilder<Key, Value> _ builder: () -> [Key: Value]) { self = builder() } }
import XCTest @testable import DictionaryBuilder final class DictionaryBuilderTests: XCTestCase { func testExample() { let dictionary: [String: Int] = .init { [ "a": 2, "b": 3, ] [ "c": 5, "d": 7, "e": 11, ] } XCTAssertEqual(dictionary, [ "a": 2, "b": 3, "c": 5, "d": 7, "e": 11, ]) } }
init
γ―γγγγγγ¨ζγ£γ¦ static func
γ«γγ¦γΏγγγ©ε€γγγγ