Skip to content

Instantly share code, notes, and snippets.

View tieleman's full-sized avatar

Sjoerd Tieleman tieleman

View GitHub Profile
private func buildComplexButton() -> some View {
if someConditional {
return Button("Press me") {
// do something
}
} else {
return Button("No, press me") {
// do something else
}
}
private func buildComplexButton() -> some View {
if someConditional {
return Button(action: { /* do something */ }) {
Text("1")
}
} else {
return Button(action: { /* do something else */ }) {
Image(systemName: "faceid")
}
}
private func buildComplexButton() -> some View {
Group {
if someConditional {
Button(action: { /* do something */ }) {
Text("1")
}
} else {
Button(action: { /* do something else */ }) {
Image(systemName: "faceid")
}
private func buildComplexButton() -> some View {
if someConditional {
return AnyView(Button(action: { /* do something */ }) {
Text("1")
})
} else {
return AnyView(Button(action: { /* do something else */ }) {
Image(systemName: "faceid")
})
}
// Put in top level file scope
extension View {
func eraseToAnyView() -> AnyView {
AnyView(self)
}
}
private func buildComplexButton() -> some View {
if someConditional {
return Button(action: { /* do something */ }) {
struct Author {
var firstName: String
var lastName: String
var booksWritten: Int
}
class AuthorViewModel {
private let service = AuthorWebService()
func allAuthors() -> [Author] {
protocol AuthorServiceProtocol {
func fetchAllAuthors() -> [Author]
}
class AuthorWebService: AuthorServiceProtocol {
func fetchAllAuthors() -> [Author] {
// fetch and return authors
}
}
class FakeAuthorService: AuthorServiceProtocol {
func fetchAllAuthors() -> [Author] {
[
Author(firstName: "Frank", lastName: "Herbert", booksWritten: 27),
Author(firstName: "Roald", lastName: "Dahl", booksWritten: 19),
Author(firstName: "Haruki", lastName: "Marukami", booksWritten: 14)
]
}
}