Skip to content

Instantly share code, notes, and snippets.

View odrobnik's full-sized avatar

Oliver Drobnik odrobnik

View GitHub Profile
@odrobnik
odrobnik / gist:2751fb3ce32792b8a85d
Last active February 21, 2023 12:06
Swift: create CGPath from attributed string
func appendToPath(path: CGMutablePath)
{
let textPath = CGPathCreateMutable()
let attributedString = NSAttributedString(string: string)
let line = CTLineCreateWithAttributedString(attributedString)
// direct cast to typed array fails for some reason
let runs = (CTLineGetGlyphRuns(line) as [AnyObject]) as! [CTRun]
for page in pages ?? []
{
guard let pageNumber = page.pageNumber
else { continue }
if let array = pageLookup[pageNumber]
{
pageLookup[pageNumber] = array + [page]
}
else
modified = @"Fri, 02 Oct 2015 16:20:34 CEST";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:locale];
_lastModifiedDate = [dateFormatter dateFromString:modified];
@odrobnik
odrobnik / gist:f2f3182fea54e07b694b
Last active November 14, 2015 15:22
Answer: how to restart animation after backgrounding
//
// HeroRaysView.swift
// HeroRays
//
// Created by Oliver Drobnik on 08/11/15.
// Copyright © 2015 Oliver Drobnik. All rights reserved.
//
import UIKit
@odrobnik
odrobnik / gist:a02ac1e49ff90ec4f8a7
Last active January 4, 2018 17:37
Upload progress reporting NSProgress for NSURLSessionTask
import Foundation
public class UploadProgress: NSProgress
{
var sessionTask: NSURLSessionTask!
required public init(sessionTask: NSURLSessionTask)
{
super.init(parent: nil, userInfo: nil)
@odrobnik
odrobnik / gist:3ecde0247717ef8f00eb
Created November 9, 2015 10:50
I would like to have this be an abstract class.
class Entity: NSObject
{
var objectID: String?
var createdAt: NSDate?
var updatedAt: NSDate?
required override init()
{
}
@odrobnik
odrobnik / something.swift
Created November 5, 2015 16:15
Why does this work? what is the difference between AnyClass and T.Type? How could this be made more generic?
func classForName(name: String) -> AnyClass
{
// build namespaced name
let namespacedName = "Module." + name
return NSClassFromString(namespacedName)!
}
func nameForType<T where T: Entity>(type: T.Type) -> String
{
@odrobnik
odrobnik / something.swift
Last active November 5, 2015 15:38
Trying to dynamically init the correct class based on a string
class Entity
{
var objectID: String?
var createdAt: NSDate?
var updatedAt: NSDate?
var avatarURL: NSURL?
var wallpaperURL: NSURL?
required init()
{
@odrobnik
odrobnik / foo.swift
Last active September 25, 2015 10:41
import Foundation
func checkIt(nameString: String, dictionary: [String: String]) -> Bool
{
let namePieces = nameString.componentsSeparatedByString("/")
guard namePieces.count > 2,
let urlString = dictionary["url"],
url = NSURL(string: urlString),
extension String
{
func isSomeText() -> Bool
{
if count(self)>0
{
return true
}
return false