Skip to content

Instantly share code, notes, and snippets.

@beccadax
beccadax / NSNotificationCenter+block.swift
Last active November 29, 2017 09:56
A safer block-based NSNotification observer API. This method automatically uses an unowned reference to the observing object, and automatically unregisters the observer when it's deallocated. This implementation is kind of a toy, but a useful toy.
// Note: You could just as easily write this in Objective-C, but I didn't want to. So nyeh.
//
// Typical usage:
// NSNotificationCenter.defaultCenter().addObserver(self, name: SomeNotification, object: someObject) { self_, note in
// ...do something with self_...
// }
//
// It would benefit from being allowed to shadow `self` in a block parameter list, but Swift
// forbids that. (http://www.openradar.me/18550356)
@bwhiteley
bwhiteley / gist:049e4bede49e71a6d2e2
Last active March 17, 2024 13:10
Initialize Swift subclass of UIView, designed in .xib
// Create CustomView.xib, set File's Owner to CustomView.
// Link the top level view in the XIB to the contentView outlet.
class CustomView : UIView {
@IBOutlet private var contentView:UIView?
// other outlets
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
self.commonInit()
@tijme
tijme / UITextViewPlaceholder.swift
Last active November 5, 2024 05:49
The correct way to implement a placeholder in a UITextView (Swift)
//
// UITextViewPlaceholder.swift
// TextViewPlaceholder
//
// Copyright (c) 2017 Tijme Gommers <tijme@finnwea.com>
//
// 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
@jinthagerman
jinthagerman / DateHelpers.swift
Created February 26, 2017 10:26
Time ago/ahead in Swift
import Foundation
struct DateComponentUnitFormatter {
private struct DateComponentUnitFormat {
let unit: Calendar.Component
let singularUnit: String
let pluralUnit: String
@tlhunter
tlhunter / average-geolocation.js
Created May 17, 2017 18:00
Calculate the center/average of multiple GeoLocation coordinates
/**
* Calculate the center/average of multiple GeoLocation coordinates
* Expects an array of objects with .latitude and .longitude properties
*
* @url http://stackoverflow.com/a/14231286/538646
*/
function averageGeolocation(coords) {
if (coords.length === 1) {
return coords[0];
}
@MaherBhasvar
MaherBhasvar / ViewController.swift
Created July 25, 2019 20:47
Adding Virtual Objects on Planes in Augmented Reality, made with ARKit in Swift
//
// ViewController.swift
// Practice-Adding-Plane
//
// Created by Maher Bhavsar on 25/07/19.
// Copyright © 2019 Seven Dots. All rights reserved.
//
import UIKit
import SceneKit