Skip to content

Instantly share code, notes, and snippets.

@yannxou
yannxou / gist:dbc0c4223a6b11c2ac6c
Last active August 29, 2015 14:10
Swift: Class method that gets called when the class is loaded for the first time
class Test: NSObject { // class must extend NSObject
override class func load() {
println("class loaded!");
}
}
@yannxou
yannxou / gist:0242a905a613fdc5578a
Last active August 29, 2015 14:10
Swift: Remove all items in an Array that are present in another Array
array = array.filter( { find(anotherArray, $0) == nil })
@yannxou
yannxou / gist:48c89c8e279e7c57e62f
Last active August 29, 2015 14:11
AppleScript: Get Current Path
-- Note when used from Automator: This returns the .app folder when saved as an application but returns the Applications folder when the workflow it's run from Automator.
tell application "Finder"
set current_path to container of (path to me) as alias
end tell
@yannxou
yannxou / gist:c75de607e8715f5a9f54
Created December 12, 2014 10:43
AppleScript: Display a list with all files in Desktop
set listOfNames to {}
tell application "Finder"
set filelist to every file of the desktop
repeat with currentFile in filelist
set currentFileName to (the name of currentFile)
copy currentFileName to the end of listOfNames
end repeat
end tell
choose from list listOfNames
@yannxou
yannxou / gist:73fba6a4df515462a041
Created December 24, 2014 09:33
Swift: Computed lazy variable example
private lazy var debugLabel: UILabel = {
let label = UILabel(frame: CGRectMake(20, 20, 200, 20))
label.textColor = UIColor.redColor()
label.backgroundColor = UIColor.whiteColor()
return label
}() // the parentheses tells swift to use the return value of the closure, not the closure itself.
@yannxou
yannxou / gist:b4f90cd144eee87f827e
Last active August 29, 2015 14:15
Swift: Find if rect contains a point by overloading equatable operator
func == (lhs: CGPoint, rhs: CGRect) -> Bool {
return CGRectContainsPoint(rhs, lhs)
}
// Example
var p1 = CGPoint(x: 10, y: 10)
var p2 = CGPoint(x: 10, y: 200)
var rect = CGRect(x: 0, y: 0, width: 100, height: 100)
if (p1 == rect) {
@yannxou
yannxou / gist:2e93dceff309c9153f19
Created May 3, 2015 22:05
Macro to easily bound a number within an upper/lower range
#define BOUND(VALUE, UPPER, LOWER) MIN(MAX(VALUE, LOWER), UPPER)
@yannxou
yannxou / RedownloadPods.sh
Last active September 9, 2015 20:49
Cocoapods: Force redownloading dependencies
rm Pods/Manifest.lock && rm Podfile.lock && pod install
@yannxou
yannxou / gist:e76b19695d670306d559
Last active October 19, 2015 07:17
Swift 2: Function documentation example
/// Swift 2 function documentation example
/**
Documentation with optional Markdown syntax
- returns: Bool
- parameter param1:String
- parameter param2:String
- Throws: error lists
*/
private func function(param1: String, param2: String) -> Bool {
@yannxou
yannxou / gist:40bb29b28f89ee1a6e71
Last active October 19, 2015 07:26
Swift: Singleton object example
/// Swift 2 syntax
class MyManager: NSObject {
static let sharedInstance = MyManager()
// Ensure the class cannot be initialized from outside by declaring a private initializer
private override init() {
super.init()
// Do your internal initialization privately
}