Skip to content

Instantly share code, notes, and snippets.

@mattt
mattt / regex.swift
Created August 11, 2014 18:08
Creating a regular expression object from a String literal
class Regex {
let pattern: String
let options: NSRegularExpressionOptions!
private var matcher: NSRegularExpression {
return NSRegularExpression(pattern: self.pattern, options: nil, error: nil)
}
required init(pattern: String, options: NSRegularExpressionOptions = nil) {
self.pattern = pattern
@mattt
mattt / もじれつ.swift
Last active August 29, 2015 14:05
Terrible misuse of Swift literal convertibles to automatically create Hiragana transliteration of string value.
struct もじれつ: Printable {
let description: String
init(string: String) {
var mutableString = NSMutableString(string: string) as CFMutableString
if CFStringTransform(mutableString, nil, kCFStringTransformLatinHiragana, 0) == 1 {
self.description = mutableString as NSString
} else {
self.description = string
}

ASCIIwwdc Viewing Statistics

June 1, 2014 – September 15, 2014

Percentage of total visits for sessions among the top 100 pages on ASCIIwwdc.

Initial Takeaways

  • Vast majority of views for 2014 sessions, as might be expected
  • Top 6 most-watched session all involve view controllers & Interface Builder (accounting for 1/4 of total traffic)
  • "What's New in X" sessions are extremely popular
enum ComparisonOrdering: Int {
case Ascending = 1
case Descending = -1
case Same = 0
}
infix operator <=> { precedence 130 }
protocol CombinedComparable: Comparable, Equatable {
func <=>(lhs: Self, rhs: Self) -> ComparisonOrdering
}
//
// RecipePageRenderer.swift
// (c) 2014 Nate Cook, licensed under the MIT License
//
import UIKit
import AVFoundation
/// Units for printing content insets
let POINTS_PER_INCH: CGFloat = 72
@mattt
mattt / nshipster-new-years-2015.md
Created November 25, 2014 19:38
NSHipster New Year's 2015

Season's Greetings, NSHipsters!

As the year winds down, and we take a moment to reflect on our experiences over the past months, one thing is clear: 2014 has been an incredible year professionally for Apple developers. So much has happened in such a short timespan, and yet it's hard to remember our relationship to Objective-C before Swift, or what APIs could have captivated our imagination as much as iOS 8 or WatchKit.

It's an NSHipster tradition to ask you, dear readers, to send in your favorite tips and tricks from the past year for publication over the New Year's holiday. This year, with the deluge of new developments—both from Cupertino and the community at large—there should be no shortage of interesting tidbits to share.

Submit your favorite piece of Swift or Objective-C trivia, framework arcana, hidden Xcode feature, or anything else you think is cool, and you could have it featured in the year-end blowout article. Just comment on this gist below!

If you're wondering about what to post, look to

@mattt
mattt / NSDecimalNumber.swift
Last active April 1, 2023 00:06
NSDecimalNumber Additions for Swift
import Foundation
// MARK: - Comparable
extension NSDecimalNumber: Comparable {}
public func ==(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool {
return lhs.compare(rhs) == .OrderedSame
}
import Darwin
extension Int {
static func random() -> Int {
return Int(arc4random())
}
static func random(range: Range<Int>) -> Int {
return Int(arc4random_uniform(UInt32(range.endIndex - range.startIndex))) + range.startIndex
}
struct Complex<T: FloatLiteralConvertible> {
var real: T
var imaginary: T
}
func +(lhs: Complex<Double>, rhs: Complex<Double>) -> Complex<Double> {
return Complex<Double>(real: lhs.real + rhs.real, imaginary: lhs.imaginary + rhs.imaginary)
}
struct URL {
var scheme: String?
var user: String?
var password: String?
var host: String?
var port: Int?
var path: String?
var query: String?
var fragment: String?