Skip to content

Instantly share code, notes, and snippets.

View MartinJNash's full-sized avatar

Martin Nash MartinJNash

View GitHub Profile
ALAssetRepresentation *rep = [asset defaultRepresentation];
NSDictionary *dict = [rep metadata];
NSString *xmp = dict[@"AdjustmentXMP"];
NSData *xmpData = [xmp dataUsingEncoding:NSUTF8StringEncoding];
NSError *e = nil;
NSArray *filters = [CIFilter filterArrayFromSerializedXMP:xmpData inputImageExtent:CGRectZero error:&e];
CIFilter *filter = [filters firstObject];
CIImage *image =[CIImage imageWithCGImage: [rep fullScreenImage]];
[filter setValue:image forKey:kCIInputImageKey];
@MartinJNash
MartinJNash / gist:3ce49f4ae48190bc1fe1
Created June 7, 2014 19:07
Swift prepare for segue
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!)
{
if let vc = segue.destinationViewController as? CreateNoteViewController {
println("Create Note VC")
}
if let vc = segue.destinationViewController as? SingleNoteViewController {
println("Show single note")
}
}
@MartinJNash
MartinJNash / DateStringMethods.swift
Last active August 29, 2015 14:04
Swift Date & String Methods
extension NSString {
func toDateWithFormatter(dateFormatter: NSDateFormatter) -> NSDate? {
return dateFormatter.dateFromString(self)
}
}
@MartinJNash
MartinJNash / ArrayByTwo.swift
Created September 12, 2014 17:50
Get adjacent pairs in an array.
extension Array {
func twoAtATime(block: ((f: T, s: T))->()) {
var _first: T? = nil
var _second: T? = nil
for e:T in self {
_first = _second
_second = e
if _first != nil && _second != nil {
struct Dispatch {
static func onMainQueue(block:()->()) {
onMainQueueWithDelay(0, block: block)
}
static func onMainQueueWithDelay(delay: NSTimeInterval, block:()->()) {
var queue: dispatch_queue_t = dispatch_get_main_queue()
onQueue(queue, withDelay: delay, block: block)
}
@MartinJNash
MartinJNash / arrayBatch.swift
Last active August 29, 2015 14:08
Batching Swift Arrays
extension Array {
func elementsInRange(start: Int, length: Int) -> Slice<T>? {
if start >= self.count {
return nil
}
var lastIndex = start + length
if start + length > self.count {
@MartinJNash
MartinJNash / NSFetchedResultsController+LinearCollection.swift
Last active August 29, 2015 14:09
Treat a sectioned NSFetchedResults controller as a flat or linear collection.
import CoreData
extension NSFetchedResultsController {
var indexPaths: [NSIndexPath] {
var paths = [NSIndexPath]()
var sectionNumber = 0
for singleSection in sections as [NSFetchedResultsSectionInfo] {
@MartinJNash
MartinJNash / Array+Repetition.swift
Created December 30, 2014 03:48
Repetition of array elements
extension Array {
/// [1, 2, 3] by 2 produces: [1, 1, 2, 2, 3, 3]
func replicated(times: UInt) -> Array {
return self.reduce([], combine: { mem, curr in
mem + Array(count: Int(times), repeatedValue: curr)
})
}
@MartinJNash
MartinJNash / Dictionary+Extensions.swift
Created January 12, 2015 16:33
Functional methods for Swift dictionaries
extension Dictionary {
func map<T>(tx: Element->T) -> [T] {
var memory: [T] = []
for (k, v) in self {
memory.append(tx(k, v))
}
return memory
}
@MartinJNash
MartinJNash / NoMoveWindow.swift
Created March 27, 2015 17:59
Hide NSDocument's "move to" titlebar button
import AppKit
class NoMoveWindow: NSWindow {
override class func standardWindowButton(b: NSWindowButton, forStyleMask m: Int) -> NSButton? {
if b == NSWindowButton.DocumentVersionsButton {
return nil
}