Skip to content

Instantly share code, notes, and snippets.

@steakknife
steakknife / NSObject+PublicClass.swift
Last active Mar 4, 2016
Determine if a random NSObject subclass instance is a public or private framework (does not test for your objects)
View NSObject+PublicClass.swift
import Foundation
extension NSObject {
var isPublicClass: Bool {
return self.dynamicType.isPublicClass
}
class var isPublicClass: Bool {
return _PUBLIC_IOS_CLASSES.contains(NSStringFromClass(self))
}
@sharplet
sharplet / LineSequence.swift
Created Nov 8, 2017
Getting a sequence of lines from a Swift string
View LineSequence.swift
import Foundation
let text = """
Hello
World
"""
extension String {
var lines: AnySequence<Substring> {
let string = self
@natecook1000
natecook1000 / nshipster-new-years-2016.md
Last active Jul 10, 2018
NSHipster New Year's 2016
View nshipster-new-years-2016.md

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].

@russbishop
russbishop / TypeErasure.swift
Last active Aug 2, 2018
Type erasure with multiple adopting types
View TypeErasure.swift
// Paste me into a playground!
import Cocoa
//: # Basic Setup
protocol FancyProtocol {
associatedtype Thing
func holdPinkyUp(x: Thing)
}
@sindresorhus
sindresorhus / StatusBarButton.swift
Last active Sep 21, 2018
Creates a NSStatusBarButton with proxy methods for the NSStatusItem methods, so you don't have to deal with that class anymore. Most of the NSStatusItem properties are deprecated, so it's much nicer to deal with the NSStatusBarButton directly.
View StatusBarButton.swift
final class AssociatedObject<T: Any> {
subscript(index: Any) -> T? {
get {
return objc_getAssociatedObject(index, Unmanaged.passUnretained(self).toOpaque()) as! T?
} set {
objc_setAssociatedObject(index, Unmanaged.passUnretained(self).toOpaque(), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
@johnno1962
johnno1962 / swiftui.swift
Last active Nov 6, 2019
SwiftUI Injection
View swiftui.swift
// 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
View erasure.swift
import Swift
/*:
A simple type-erased sequence
*/
let seq = AnySequence([1,2,3])
/*:
## Who Needs Types Like That?
@AvdLee
AvdLee / MutableProgress.swift
Last active Nov 18, 2020
Adding children to a Progress instance is easy, but removing is not possible by default. This could have been useful when you want to use a single Progress instance which can have different children over time. Using this custom class MutableProgress makes this possible. See: https://www.avanderlee.com/general/controlling-progress-children/
View MutableProgress.swift
import UIKit
/// An additional class build on top of `Progress` to make it possible to also remove children from progress.
public final class MutableProgress: Progress {
public override var totalUnitCount: Int64 {
get {
return Int64(children.count)
}
set {
@shaps80
shaps80 / Models.swift
Last active Jan 9, 2021
Swift type for representing a UserAgent (includes an implementation similar of Apple’s Version from SPM)
View Models.swift
import UIKit
extension UIDevice {
/*
List can be updated here:
https://gist.github.com/adamawolf/3048717
*/
internal static var models: String = """
@zwaldowski
zwaldowski / Activity.swift
Last active Jan 14, 2021
os_activity_t for Swift 3
View Activity.swift
//
// Activity.swift
//
// Created by Zachary Waldowski on 8/21/16.
// Copyright © 2016 Zachary Waldowski. Licensed under MIT.
//
import os.activity
private final class LegacyActivityContext {