Skip to content

Instantly share code, notes, and snippets.

View joshuawright11's full-sized avatar
🧪

Josh Wright joshuawright11

🧪
View GitHub Profile
struct UploadAnalyticsJob: Job {
func run() async throws {
var allEvents: [AnalyticsEvent] = []
allEvents.append(contentsOf: try await CardEvent.all().map(\.event))
}
}
extension CardEvent {
fileprivate var event: AnalyticsEvent {
.cardEvent(
@joshuawright11
joshuawright11 / Binding.swift
Last active January 13, 2022 04:36
Binding
let binding: Binding<Bool>? = ... // Note this is Binding<Bool>? Not Binding<Bool?>.
if let binding = binding { // now "unwrapped" to Binding<Bool>
SuccessView(binding: binding)
}
@joshuawright11
joshuawright11 / AttributedStrings.swift
Last active May 4, 2021 00:55
Swift attributed strings with @resultBuilder
import UIKit
public protocol AttributedStringConvertible {
var attributedString: NSMutableAttributedString { get }
}
extension String: AttributedStringConvertible {
public var attributedString: NSMutableAttributedString {
NSMutableAttributedString(string: self)
}
@joshuawright11
joshuawright11 / migration.swift
Last active August 13, 2020 06:02
Sample migration
struct Sample: Migration {
func up(schema: Schema) {
schema.rename(table: "todos", to: "user_todos")
schema.create(table: "users") {
$0.uuid("id").primary()
$0.string("name").nullable(false)
$0.string("email").nullable(false).unique().index()
$0.uuid("mom").references("id", on: "users")
}
schema.drop(table: "referrals")
@joshuawright11
joshuawright11 / TripModel.swift
Created March 9, 2020 17:09
DB wrapper for `Trip`
import FluentPostgresDriver
import RoamShared
import Vapor
final class TripModel: Model {
static var schema: String = "trips"
@ID(key: .id)
var id: UUID?
public class ZeroConstraint: NSLayoutConstraint {
public override var constant: CGFloat {
get {
let adjustedConstraint = super.constant * SizeAdjustment.percentage
return adjustedConstraint
}
set { super.constant = newValue }
}
}

Device Logs:

default	17:45:21.522408 -0700	installd	Trust evaluate failure: [leaf IssuerCommonName LeafMarkerOid SubjectCommonName]
default	17:45:21.523628 -0700	installd	elided platform fast path for key: re6Zb+zwFKJNlkQTUeT+/w
default	17:45:21.533757 -0700	installd	Skipping a profile because of error 0xe8008012.
default	17:45:21.534225 -0700	installd	Skipping a profile because of error 0xe8008012.
default	17:45:21.534625 -0700	installd	Skipping a profile because of error 0xe8008012.
default	17:45:21.536239 -0700	installd	Skipping a profile because of error 0xe8008012.
default	17:45:21.536294 -0700	installd	Skipping a profile because of error 0xe8008012.
default	17:45:21.536564 -0700	installd	Skipping a profile because of error 0xe8008012.
class TransactionStore {
var shared: TransactionStore = TransactionStore()
var transactions: Variable<[Transaction]>
}
// ViewModel
class Presenter {

Control Structures

Control structures are things you can put in your code to perform various logic operations. (i.e. perform a line of code multiple times)

There are a few but there are 2 important ones to know:

  • if statments
  • for loops

if Statements

Functions

Each function has

  • parameters (what variables the function takes)
  • return type (what the fucntion returns, if anything)
// No return type
func printNum(num: Int) {
 print(num)