Skip to content

Instantly share code, notes, and snippets.

View danielctull's full-sized avatar

Daniel Tull danielctull

View GitHub Profile
@schwa
schwa / SharedWindow.swift
Created December 15, 2023 19:28
SwiftUI example showing how to make a Shared Content Window…
import SwiftUI
import Observation
import os
/// A trick in SwiftUI to have a singls shared global window.
/// We want the user to be able to show and hide this window, and change the content in it. Hitting show multiply times should NOT open multiple windows but instead should bring the shared window forward.
/// The trick is to use a dummy Singleton type (here we embed it with the shared content) that we can pass to open/dismissWindow and the WindowGroup. The dummy SIngleton type must contain no properties and merely exists to trick SwiftUI into only ever showing one window for the type at a time.
@Observable
class SharedContent {
@krzyzanowskim
krzyzanowskim / PastelColor.swift
Last active February 10, 2024 10:27
Random pastel colors
struct ContentView: View {
@State var color: Color = .Pastel.random()
var body: some View {
Rectangle()
.foregroundColor(color)
.onTapGesture {
color = .Pastel.random()
}
}
@mergesort
mergesort / AnimationDemo.swift
Last active December 18, 2023 02:34
A SwiftUI prototype animation for adding/removing players from a game
import SwiftUI
let colors = [
Color.pink,
Color.blue,
Color.green,
Color.orange,
Color.purple,
Color.black,
]
title synopsis tags share_image episode book collection author
Swift Tip: Protocols vs. Values
Extensible in different ways
news
/images/blog/2019-05-13-protocols-vs-functions.png
19
advanced-swift
networking
chriseidhof
extension URLRequest {
private func quote(_ string: String) -> String {
// handle all the regular escape sequences
var text = string.replacingOccurrences(of: "\\", with: "\\\\")
text = text.replacingOccurrences(of: "\"", with: "\\\"")
text = text.replacingOccurrences(of: "\n", with: "\\\n")
text = text.replacingOccurrences(of: "\r", with: "\\\r")
text = text.replacingOccurrences(of: "\t", with: "\\\t")
return "\"\(text)\""
}
@alexisakers
alexisakers / CFArray+Sequence.swift
Created December 22, 2016 18:59
Extension on the CFArray type to make it conform to Sequence
/*
* CFArray+Sequence.swift
* Alexis Aubry Radanovic
*/
import Foundation
import CoreFoundation
extension CFArray: Sequence {
@steipete
steipete / gist:99f8aa8d6527fa027fd6
Last active August 29, 2015 14:25
Mirror for https://forums.developer.apple.com/thread/11098 since that needs moderation first (submitted Jul 16 2015)
We're working on supporting Bitcode in PSPDFKit but hit a blocking issue with OpenSSL (latest 1.0.2d).
We're using a script that downloads and builds OpenSSL into fat static libraries for both Mac and iOS. I've put the gist here:
https://gist.github.com/steipete/ce09ba176a4b8ef66b2d/bf053eab1c0f8494dcd3748079543c4ed4367b9c
Enabling Bitcode should be as easy as adding `-fembed-bitcode` as clang flag according to this entry. That's exactly what I did.
Now things build, but the script eventually stops with this error:
ld: could not open bitcode temp file: ../libcrypto.a(aes-x86_64.o) for architecture x86_64
@andymatuschak
andymatuschak / CollectionViewDataSource.swift
Last active February 12, 2021 09:44
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
@andrewsardone
andrewsardone / gist:6458455
Created September 6, 2013 01:32
script/sort-xcodeproj for handy Xcode project organization
#!/bin/bash
#
# Usage: script/sort-xcodeproj to sort all root-level Xcode projects.
# This helps keep the Xcode project layout neat and consistent while
# aiding in the prevention of [merge conflicts][mc].
#
# [mc]: http://danieltull.co.uk/blog/2013/09/05/easier-merging-of-xcode-project-files/
SCRIPT_DIR=$(dirname "$0")
cd "$SCRIPT_DIR/.."
NSManagedObjectContext *contextOne = ...;
NSArray *objects = ...<result of fetch request on contextOne...
NSManagedObjectID *objectID = objects[0].objectID;
NSManagedObjectContext *contextTwo = ...;
id object = [contextTwo existingObjectWithID:objectID error:nil]; // returns the right object
NSAssert(![object.objectID isTemporaryID]); // passes
id otherObject = [contextTwo objectWithID:objectID]; // always returns an object, should be the right object
NSAssert(![otherObject.objectID isTemporaryID]); // fails!