Skip to content

Instantly share code, notes, and snippets.

struct MyStruct {
var value: Int = 0
}
var instanceOfMyStruct = MyStruct(value: 42)
var assignedInstance1 = instanceOfMyStruct
assignedInstance1.value = 2 // Change the copied instance
instanceOfMyStruct.value // 42: Original is unchanged
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
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)
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) {
@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
@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 / 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
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 / 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 }
/* 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 }
}