Skip to content

Instantly share code, notes, and snippets.

import SwiftUI
struct NumbersCollectionView : UIViewRepresentable {
@Binding var numbers: [Int]
func makeUIView(context: Context) -> UICollectionView {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = .white
@allevato
allevato / Transient.swift
Created September 12, 2017 14:21
Wrapper for transient properties to use with synthesized protocol defaults
/// Wraps a value but causes it to be treated as "value-less" for the purposes
/// of automatic Equatable, Hashable, and Codable synthesis. This allows one to
/// declare a "cache"-like property in a value type without giving up the rest
/// of the benefits of synthesis.
public enum Transient<Wrapped>: Equatable, Hashable, Codable {
case none
case some(Wrapped)
public static func == (lhs: Transient<Wrapped>, rhs: Transient<Wrapped>) -> Bool {

Note

Apple will reject apps that are using private url schemes (Ugh, Apple....) if they are pretty much obvius. Some apps are rejected and others are not, so, be aware of this issue before implementing any of those URL's in your app as a feature.

Updates

  • [UPDATE 4] iOS 10 update: apparently settings now can be reached using App-Pref instead of prefs
  • [UPDATE 3] For now you just can use url schemes to open your apps's settings with Swift 3.0 (Xcode 8). I'll keep you informed when OS preferences can be reached
  • [UPDATE 2] The openURL() method of UIApplication is now deprecated. You should use application(_:open:options:) instead
  • [UPDATE 1] Not yet tested in iOS 10. It will fail because of policies changes in URL scheme handling.
@onecrayon
onecrayon / dropbox-git-init.sh
Created October 4, 2016 21:08
Bash script for automatically creating a bare git repo in Dropbox (for tracking pre-existing private repo)
##
# This script adds a new git repo to Dropbox (named after the current working
# directory), adds it as a remote, and pushes everything to it. If there is an
# argument, then that will be used as the name of the repo instead of the
# current working directory.
#
# EXAMPLE USAGE:
#
# $ cd existing-repo
# $ /path/to/dropbox-git-init.sh
/// Additional colors accessibility metrics utilities
import Cocoa
import simd
extension NSColor {
/// Returns in an ordered array the following components from this color, in the sRGB color space:
/// - red
/// - green
static inline const uint8_t *DecodeOneUTF8(const uint8_t *utf8, uint32_t *outCodepoint) {
uint8_t byteOne = utf8[0];
uint8_t byteTwo = utf8[1];
uint8_t byteThree = utf8[2];
uint8_t byteFour = utf8[3];
uint8_t bit1 = byteOne >> 7;
// uint8_t bit2 = (byteOne >> 6) & 1;
uint8_t bit3 = (byteOne >> 5) & 1;
uint8_t bit4 = (byteOne >> 4) & 1;
import Cocoa
enum CoroutineState {
case Fresh, Running, Blocked, Canceled, Done
}
struct CoroutineCancellation: ErrorType {}
class CoroutineImpl<InputType, YieldType> {
let body: (yield: YieldType throws -> InputType) throws -> Void
let task = NSTask()
task.launchPath = "/bin/sh"
task.arguments = ["-c", "sleep 0.2; open \"\(NSBundle.mainBundle().bundlePath)\""]
task.launch()
NSApplication.sharedApplication().terminate(nil)
struct Regex {
let pattern: String
let options: NSRegularExpressionOptions!
private var matcher: NSRegularExpression {
return NSRegularExpression(pattern: self.pattern, options: self.options, error: nil)
}
init(pattern: String, options: NSRegularExpressionOptions = nil) {
self.pattern = pattern
@kerker00
kerker00 / gist:63d52c7b0e6a5df1d2bc
Last active October 3, 2018 19:16
Self relaunch of Cocoa application (Swift version of https://gist.github.com/cdfmr/2204627)
var task = NSTask()
var args: NSMutableArray
args = NSMutableArray()
args.addObject("-c")
args.addObject("sleep 0.2; open \"\(NSBundle.mainBundle().bundlePath())\"")
task.setLaunchPath("/bin/sh")
task.setArguments(args)
task.launch()