Skip to content

Instantly share code, notes, and snippets.

View jorgenisaksson's full-sized avatar

Jörgen Isaksson jorgenisaksson

View GitHub Profile
@jorgenisaksson
jorgenisaksson / StringExtensions.swift
Last active April 3, 2017 08:54
String extension to localize strings with parameter support
extension String
{
public static func localize(_ key: String, comment: String) -> String {
return NSLocalizedString(key, comment: comment)
}
func format(parameters: CVarArg...) -> String {
return String(format: self, arguments: parameters)
}
}
@jorgenisaksson
jorgenisaksson / StructCoding.swift
Created April 1, 2017 13:22
NSCoding support for Swift Structs. Save and load your Swift Structs from disk!
import UIKit
public struct PersonItem
{
var title: String
init(title: String) {
self.title = title
}
}
@jorgenisaksson
jorgenisaksson / StringExtensions.swift
Last active April 3, 2017 08:45
Uppercase the first character of a Swift string
import Foundation
extension String {
func firstCharacterUpperCase() -> String {
if let firstCharacter = characters.first, characters.count > 0 {
return replacingCharacters(in: startIndex ..< index(after: startIndex), with: String(firstCharacter).uppercased())
}
return self
}
@jorgenisaksson
jorgenisaksson / ConformsToProtol.swift
Last active March 24, 2017 13:26
Check if a class conforms to a protocol instead of using respondsToSelector like in obj-c
import UIKit
// the protocol
protocol LayerManagerDelegate: class {
func doSomething()
}
// the class
class LayerManager {
@jorgenisaksson
jorgenisaksson / gist:76a8dae54fd3dc4e31c2
Created June 10, 2014 18:01
Create a CGPath from an NSBezierPath in Swift. Great for CALayers for example.
// Adapted from Cocoa Drawing Guide's "Create a CGPathRef fram an NSBezierPath Object"
func CGPathFromNSBezierPath(nsPath: NSBezierPath) -> CGPath! {
if nsPath.elementCount == 0 {
return nil
}
let path = CGPathCreateMutable()
var didClosePath = false