Skip to content

Constructing a Struct using the FunctionBuilder added in Swift5.1

   

This is how to create a Struct using the FunctionBuilder added in Swift 5.1. It's also active in SwiftUI.

public struct Company: CustomStringConvertible {
let name: String
let president: String
let numberOfEmployees: Int
let adress: String
public var description: String {
return "name: \(name), president: \(president), numberOfEmployees: \(numberOfEmployees), adress: \(adress)"
}
}
@_functionBuilder public struct CompanyBuilder {
public static func buildBlock(_ name: String, _ president: String, _ numberOfEmployees: Int, _ adress: String) -> Company {
Company(name: name, president: president, numberOfEmployees: numberOfEmployees, adress: adress)
}
}
@CompanyBuilder
func myFunc() -> Company {
"株式会社すいすいSwift"
"カビゴン小野"
1
"神奈川"
}
let company = myFunc()
print(company.description)

  1. Using TabView to switch screens (SwiftUI)
  2. Transition from one ListView to another (SwiftUI)
  3. Parsing the Json received in Alamofire using Codable
  4. SwiftUI gets used a lot with Struct
  5. Displaying a List in SwiftUI