Skip to content

Instantly share code, notes, and snippets.

Avatar
🦅
Hacking in Swift

Adrian M. DevAndArtist

🦅
Hacking in Swift
View GitHub Profile
View labeled_typeless_bridges.md
struct A {

  bridge bridge_name {
    
    func foo() {}
  }
}

let a = A()
View vectors_tuples_and_more.md

Vectors

  • A vector contains at least one type.
  • vector T - is a list of type T of an arbitrary length, such as T1, T2, ...
  • vector(n) T - is a list with the length of maximal n T's, such as T1, T2, ... , Tn
// vectorized tuple with arbitrary length (like an existetial that can accept a tuple of type `T` of any length)
var t1: (vector Int) = (1, 2)
t1 = (1, 2, 3)
View simple-multi-line-string-literal-model.md

Simple 'multi-line string literal' model

Core features:

  1. Omitting of (most) backslashes for ".
  2. Altering the string with implicit new line injection at the end of the line.
  3. Sting interpolation (trivial in terms of consistency and functionality).

Consequences of #1:

View long-string.swift
// Currently it would look like this:
let myLongString = "Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.\n\nNam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.\n\nDuis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis."
// With the accepted version
View UIColor+Grayscale.swift
extension UIColor {
public var grayscale: UIColor {
var (red, green, blue, alpha) = (CGFloat(0), CGFloat(0), CGFloat(0), CGFloat(0))
if self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
// https://en.wikipedia.org/wiki/Grayscale
return UIColor(white: red * 0.299 + green * 0.587 + blue * 0.114, alpha: alpha)
View OptionSet-Enum.swift
public enum Foo : OptionSet {
case a
case b
case value(UInt8)
public var rawValue: UInt8 {
switch self {
case .a:
@DevAndArtist
DevAndArtist / swift-string-literal.md
Last active May 18, 2017
Aligned String Literal Model in Swift
View swift-string-literal.md

Motivation

In Swift 3 the string literal is a simple stream of characters that is enclosed by starting and ending " delimiters without new lines after and before the delimiters.

// Example #1
"\"Title\"\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Integer elementum commodo sem, a congue orci porta sit amet.\nDuis facilisis, est et vehicula congue, turpis dui ultricies nunc, ut elementum quam elit nec felis."

This issue makes it very hard to write code that has a fixed line width.

View swift-string-literal.md
print("\"text\" \"text\" \"text\" text text text text text text text text text\ntext text text text text text text")

Allow:

print("
  \"text\" \"text\" \"text\" text text text text \
 text text text text text\n
View gist:ee36db341c37b8726e4af651c573771b
//
// ViewController.swift
// test
//
// Created by Andrew Carter on 6/7/17.
// Copyright © 2017 Andrew Carter. All rights reserved.
//
import UIKit
@DevAndArtist
DevAndArtist / guardedAreaLayoutGuide.md
Created Sep 13, 2017
A layout guide for iOS 10 that is very similar to the `safeAreaLayoutGuide` from iOS 11.
View guardedAreaLayoutGuide.md

A layout guide for iOS 10 that is very similar to the safeAreaLayoutGuide from iOS 11.

extension UIView {
	///
	private final class LayoutGuide : UILayoutGuide {}
	
	///
	var guardedAreaLayoutGuide: UILayoutGuide {
		if #available(iOS 11, *) {