Skip to content

Instantly share code, notes, and snippets.

View mattmassicotte's full-sized avatar

Matt Massicotte mattmassicotte

View GitHub Profile

Set value

GCC_PREPROCESSOR_DEFINITIONS = FOO=My Value

Add Constants.m

It is particularly important to check that the definition of your value exists here, as it will not cause a compilation failure, but instead result in the generation of @"FOO".

@import Foundation;
import SwiftUI
import AsyncAlgorithms
struct AsyncChanges<V>: ViewModifier where V : Equatable, V: Sendable {
typealias Element = (oldValue: V, newValue: V)
typealias Action = (AsyncStream<Element>) async -> Void
@State private var streamPair = AsyncStream<Element>.makeStream()
private let action: Action
private let value: V
@mattmassicotte
mattmassicotte / UsesAsyncSequence.swift
Created January 26, 2024 11:52
MainActor + AsyncSequence
@MainActor
final class UsesAsyncSequence {
func original() {
let stream = AsyncStream { 42 }
Task {
// Passing argument of non-sendable type
// 'inout AsyncStream<Int>.Iterator' outside of main
// actor-isolated context may introduce data races
for await value in stream {
@mattmassicotte
mattmassicotte / license.diff
Created December 31, 2023 12:44
Modified 3-Clause BSD
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+1. Redistributions of source code must retain the above copyright notice, this list of conditions, a reference to the original project, and the following disclaimer.
-2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, a reference to the original project, and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products deri
@mattmassicotte
mattmassicotte / nonglobalactor.swift
Created October 25, 2023 15:14
Use non-global actor protocols with global actor types
import AppKit
// Make a wrapper for the delegate that maps delegate methods to functions
// (Making it a class/inherit from NSObject as needed)
final class TextLayoutManagerDelegateAdapter: NSObject {
public typealias TextLayoutFragmentProvider = (_ location: NSTextLocation, _ textElement: NSTextElement) -> NSTextLayoutFragment
public let textLayoutFragmentProvider: TextLayoutFragmentProvider
public init(
@mattmassicotte
mattmassicotte / SendableCapture.swift
Created September 22, 2023 10:30
Capturing a non-Sendable in a @sendable closure that produces no warning
struct NonSendable {
}
@available(*, unavailable)
extension NonSendable: Sendable {
}
actor MyActor {
let nonSendable: NonSendable
@mattmassicotte
mattmassicotte / AsyncMap.swift
Created June 18, 2022 11:00
Swift asyncMap function
extension Sequence {
func asyncMap<T>(_ transform: @escaping (Element) async -> T) async -> [T] {
return await withTaskGroup(of: T.self) { group in
var transformedElements = [T]()
for element in self {
group.addTask {
return await transform(element)
}
}
@mattmassicotte
mattmassicotte / shared_publisher.swift
Last active May 21, 2022 15:12
This code demonstrates that using a shared publisher will not always be able to deliver results to concurrent subscribers
import Foundation
import Combine
enum State: Int {
case starting
case firstValueReceived
case secondSubscriptionCreated
case finishedWithFirstValue
case secondValueReceived
case finished
func testCurrentYearAppearsInInfoPlistCopyright() throws {
let bundle = Bundle(for: AppDelegate.self)
let plist = try XCTUnwrap(bundle.localizedInfoDictionary)
let string = try XCTUnwrap(plist["NSHumanReadableCopyright"] as? String)
let formatter = DateFormatter()
formatter.dateFormat = "yyyy"
return (
<MultiChildComponent>
<ChildNode key="child1">
<span>Yo</span>
</ChildNode>
<ChildNode key="child2">
<span>Dawg</span>
</ChildNode>
</MultiChildComponent>
);