Skip to content

Instantly share code, notes, and snippets.

@sharplet
sharplet / LineSequence.swift
Created November 8, 2017 20:03
Getting a sequence of lines from a Swift string
import Foundation
let text = """
Hello
World
"""
extension String {
var lines: AnySequence<Substring> {
let string = self
@natecook1000
natecook1000 / nshipster-new-years-2016.md
Last active July 10, 2018 19:24
NSHipster New Year's 2016

Greetings and salutations, NSHipsters!

As the year winds down, it's a tradition here at NSHipster to ask you, dear readers, to offer up your favorite tricks and tips from the past year as gifts to your fellow hipsters. With iOS 9, El Capitan, brand new watch- and tvOS's, and the open-sourcing of some minor Apple-related tech, there's bound to be lots to share.

Submit your favorite piece of Swift or @objc trivia, helpful hints, unexpected discoveries, useful workarounds, useless fascinations, or anything else you found cool this year. Just comment below!

If you need inspiration, try [the list from last year][2015], or [from the year before][2014], or [from the year before that][2013].

@johnno1962
johnno1962 / swiftui.swift
Last active November 6, 2019 23:51
SwiftUI Injection
// First, install InjectionIII.app from the Mac AppStore
// https://apps.apple.com/us/app/injectioniii/id1380446739?mt=12
// Make these changes to your code:
// add the following to application(didFinishLaunchingWithOptions:)
#if DEBUG
Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/iOSInjection.bundle")?.load()
#endif
@rnapier
rnapier / erasure.swift
Created August 4, 2015 20:10
A Little Respect for AnySequence (http://robnapier.net/erasure)
import Swift
/*:
A simple type-erased sequence
*/
let seq = AnySequence([1,2,3])
/*:
## Who Needs Types Like That?
@shaps80
shaps80 / Models.swift
Last active January 9, 2021 16:48
Swift type for representing a UserAgent (includes an implementation similar of Apple’s Version from SPM)
import UIKit
extension UIDevice {
/*
List can be updated here:
https://gist.github.com/adamawolf/3048717
*/
internal static var models: String = """
@drewmccormack
drewmccormack / LetTheCoWGo.swift
Created February 2, 2019 10:25
Demonstrates how a Swift value constant can mutate when using Copy-on-Write (CoW) and multi-threading.
import Foundation
struct NuclearPowerStationOperator {
class Storage {
var turnOffCores: Bool = false
func copy() -> Storage {
let new = Storage()
new.turnOffCores = turnOffCores
return new
@apptekstudios
apptekstudios / CustomPicker.swift
Last active May 28, 2021 02:35
A variation on the picker designed by Federico Zanetello in his blog post (https://fivestars.blog/swiftui/inspecting-views.html) that allows any type of content to be used
//
// CustomPicker.swift
//
// Created by T Brennan on 27/3/21.
//
import SwiftUI
struct ContentView: View {
@State private var selection: Int? = 0
@seanlabastille
seanlabastille / LogReader.swift
Created July 8, 2020 09:47
Attempting to read OSLogStore in iOS 14 Beta 2
import OSLog
class LogReader: ObservableObject {
var log: OSLogStore
init?() {
do {
log = try OSLogStore.init(scope: .currentProcessIdentifier)
dump(log)
/*
//
// ProgrammerAssertions.swift
// Assertions
//
// Created by Mohamed Afifi on 12/20/15.
// Copyright © 2015 mohamede1945. All rights reserved.
//
import Foundation
@milseman
milseman / Swift5StateOfString.md
Created January 10, 2018 19:49
State of String: ABI, Performance, Ergonomics, and You!

State of String: ABI, Performance, Ergonomics, and You!

Hello, I’ve been working on implementing, optimizing, and improving String in preparation for ABI stability, and I thought I’d share the current status of String in Swift 5 and some potential directions to go. This is the product of conversations with open source contributors and my colleagues on the Swift standard library team at Apple.

The primary area of focus is stabilizing String’s ABI, but we’d be remiss if we couldn’t also fit in performance and ergonomic improvements. String’s ergonomics in particular is one area where we think the status quo is woefully inadequate, and over half of this email is devoted to that topic. At the end, there’s a section about a community initiative that we hope can help users of String as well as guide future development.

(Note: I’m sending this to swift-dev because much of the contents revolve around implementation concerns. I’ll also cross-reference on swift-evolution and swift-users. See also the [StringManife