Skip to content

Instantly share code, notes, and snippets.

@roop
roop / notes_on_debugging_the_swift_compiler.md
Last active January 21, 2020 06:55
Notes on debugging on the Swift compiler

Invoking lldb

  • swiftc invokes sub-commands, so you shouldn't run lldb on the swiftc command itself. You can add the -### option to swiftc to see the underlying sub-commands. Pick the correct sub-command, then run lldb -- <command>.

  • Usually, we want the first command, so run something like:

    lldb -- `swiftc file.swift | head -n 1`
    
  • Use --one-line r to run immediately after loading, without having to set breakopints - great for debugging crashes

/* This is an alternate responder chain implementation that
DOES NOT work in Swift 2.2, but might in a
hypothetical future version of Swift. */
protocol Command {
}
protocol ResponderChainable {
var nextResponder: ResponderChainable? { get }
}
@roop
roop / SwiftyResponderChain.swift
Created June 3, 2016 21:49
A responder chain implementation for a hypothetical pure-Swift equivalent for UIKit. More info at: http://roopc.net/posts/2016/swifty-responder-chain/
/* A responder chain implementation for a hypothetical pure-Swift
* equivalent for UIKit.
* More info at: http://roopc.net/posts/2016/swifty-responder-chain/
*/
/* A responder is something that has an optional next responder,
so that we can have a chain of responders. */
protocol Responder {
var nextResponder: Responder? { get }
protocol Feed: Equatable {
var url: String { get }
}
func ==<T: Feed>(lhs: T, rhs: T) -> Bool {
return (lhs.url == rhs.url)
}
protocol Folder {
typealias FeedType: Feed
@roop
roop / AutoLayoutHelper.swift
Created July 10, 2015 12:58
Helper for specifying AutoLayout constraints in code
/*
AutoLayoutHelper.swift is published under the MIT License.
Copyright (C) 2014-15, Roopesh Chander http://roopc.net/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@roop
roop / ObservationHelper.swift
Last active November 16, 2015 07:00
Improvised KVO
/*
ObservationHelper.swift is published under the MIT License.
Copyright (C) 2014-15, Roopesh Chander http://roopc.net/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@roop
roop / makemake.pl
Last active January 2, 2021 12:30
Script to create a Makefile to build your Swift project
#!/usr/bin/perl -w
use strict;
# Makefile generator for quick compilation of Swift projects
# By: Roopesh Chander
# Thanks: Andy Matuschak
# Works only for swift-only projects.
# Usage:
# > perl makemake.pl
# > make
extension Array {
var unsafePointerToElements: UnsafePointer<T> {
return self.withUnsafePointerToElements { return $0 }
}
subscript (index: Int) -> T {
get {
let ptr = self.unsafePointerToElements
return ptr[index]
}
set(rhs) {
extension Array {
func sharesStorageWith(other: Array<T>) -> Bool {
let thisPtr: UnsafePointer<T> = self.withUnsafePointerToElements { return $0 }
let otherPtr: UnsafePointer<T> = other.withUnsafePointerToElements { return $0 }
return (thisPtr == otherPtr)
}
}
var a1: Array<Int> = [10, 11]
a1.append(20)
class MyClass {
var value: Int
init(value v: Int) { value = v }
}
var instanceOfMyClass = MyClass(value: 42)
var assignedInstance2 = instanceOfMyClass
assignedInstance2.value = 2 // Change the copied instance
instanceOfMyClass.value // 2: Original is also changed