Skip to content

Instantly share code, notes, and snippets.

View DevAndArtist's full-sized avatar
🦅
Hacking in Swift

Adrian M. DevAndArtist

🦅
Hacking in Swift
View GitHub Profile
//
// ViewController.swift
// test
//
// Created by Andrew Carter on 6/7/17.
// Copyright © 2017 Andrew Carter. All rights reserved.
//
import UIKit
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
@DevAndArtist
DevAndArtist / swift-string-literal.md
Last active May 18, 2017 14:47
Aligned String Literal Model in Swift

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.

public enum Foo : OptionSet {
case a
case b
case value(UInt8)
public var rawValue: UInt8 {
switch self {
case .a:
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)
// 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

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:

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)
struct A {

  bridge bridge_name {
    
    func foo() {}
  }
}

let a = A()
Suggestion 1 Suggestion 2 Suggestion 3 Swift 3 Implication of #3
public open open public open open
closed public public public public public
fixed public  
final public closed final public final public final public
internal internal internal internal internal
final internal final internal final internal final internal final internal
private private private fileprivate private
final private final private final private final fileprivate final private