Skip to content

Instantly share code, notes, and snippets.

View tieleman's full-sized avatar

Sjoerd Tieleman tieleman

View GitHub Profile
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)
]
}
}
protocol AuthorServiceProtocol {
func fetchAllAuthors() -> [Author]
}
class AuthorWebService: AuthorServiceProtocol {
func fetchAllAuthors() -> [Author] {
// fetch and return authors
}
}
struct Author {
var firstName: String
var lastName: String
var booksWritten: Int
}
class AuthorViewModel {
private let service = AuthorWebService()
func allAuthors() -> [Author] {
// 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 */ }) {
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")
})
}
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 Button(action: { /* do something */ }) {
Text("1")
}
} else {
return Button(action: { /* do something else */ }) {
Image(systemName: "faceid")
}
}
private func buildComplexButton() -> some View {
if someConditional {
return Button("Press me") {
// do something
}
} else {
return Button("No, press me") {
// do something else
}
}
@tieleman
tieleman / vDSPBenchmark.swift
Last active March 12, 2020 08:34
Xcode playground code for benchmarking a Swifty implementation vs. vDSP.
import Accelerate
import XCTest
let array = Array(stride(from: 0.0, to: 50000.0, by: 1))
let array2 = Array(stride(from: 0.0, to: 50000.0, by: 1))
let array3 = Array(stride(from: 0.0, to: 50000.0, by: 1))
class MathTests: XCTestCase {
func testMeanSwifty() {
measure {
@tieleman
tieleman / multiply.swift
Created March 11, 2020 21:46
Multiplication example using vDSP
func multiply(_ array: [Double]) -> [Double] {
// Allocate memory for the output array
let output = UnsafeMutablePointer<Double>.allocate(capacity: array.count)
// Make sure we free up the memory once we're done
defer {
output.deinitialize(count: array.count)
output.deallocate()
}