Skip to content

Instantly share code, notes, and snippets.

View odrobnik's full-sized avatar

Oliver Drobnik odrobnik

View GitHub Profile
/// The current element attributes state
internal struct TagAttributes
{
var tagName: String!
var URL: NSURL?
var imageURL: NSURL?
var isUnderlined: Bool = false
var isStriken: Bool = false
extension Dictionary where Key: String, Value: NSFileWrapper
{
mutating func renameBundleResource(oldName: String, newName: String)
{
// remove extension
let prefix = (oldName as NSString).stringByDeletingPathExtension
// find all files belonging to this resource
let fileNames = keys.sort().filter { (key) -> Bool in
return key.hasPrefix(prefix)
public extension CollectionType where Generator.Element == NSIndexPath
{
func indexPathsWithSectionModified(by modifier: Int) -> [NSIndexPath]
{
var modifiedIndexPaths = [NSIndexPath]()
for indexPath in self
{
let modifiedIndexPath = NSIndexPath(forItem: indexPath.item, inSection: indexPath.section + modifier)
modifiedIndexPaths.append(modifiedIndexPath)
Pod::Spec.new do |spec|
spec.name = 'BarCodeKit'
spec.version = '1.3.1'
spec.license = 'BSD'
spec.source = { :git => 'https://github.com/Cocoanetics/BarCodeKit.git', :tag => spec.version.to_s }
spec.ios.source_files = 'Core/Source/iOS/*.{h,m}', 'Core/Source/*.{h,m}', 'Core/*.h'
spec.osx.source_files = 'Core/Source/Mac/*.{h,m}', 'Core/Source/*.{h,m}', 'Core/*.h'
spec.requires_arc = true
spec.homepage = 'http://www.cocoanetics.com/parts/barcodekit/'
spec.summary = 'A framework to generate bar codes on iOS or Mac.'
@odrobnik
odrobnik / batchdeletion.swift
Created April 25, 2016 08:46
Backwards-compatible batch deletion
extension NSPersistentStoreCoordinator
{
func batchDelete(fetchRequest: NSFetchRequest) throws
{
// create a worker
let context = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
context.persistentStoreCoordinator = self
var retError: NSError!
enum Result
{
case Success()
case Error(error: NSError)
case Properties(nodes: [WebDAVNode])
}
typealias WebDAVRequestCompletion = (Result)->()
func listDirectory(path: String, completion: WebDAVRequestCompletion?)
// Swift version of https://developer.apple.com/library/mac/technotes/tn2084/_index.html
@IBAction func testButtonPushed(sender: AnyObject) {
let URL = NSBundle.mainBundle().URLForResource("SendFinderMessage", withExtension: "scpt")!
var errors: NSDictionary?
let script = NSAppleScript(contentsOfURL: URL, error: &errors)!
@odrobnik
odrobnik / gist:e8ac59e13b62ea80b623
Created January 11, 2016 11:26
Calling AppleScript from Swift App, passing a parameter
// Swift version of https://developer.apple.com/library/mac/technotes/tn2084/_index.html
@IBAction func testButtonPushed(sender: AnyObject) {
let URL = NSBundle.mainBundle().URLForResource("SendFinderMessage", withExtension: "scpt")!
var errors: NSDictionary?
let script = NSAppleScript(contentsOfURL: URL, error: &errors)!
@odrobnik
odrobnik / gist:f32ded1fce2d168b6b66
Created December 16, 2015 12:42
Enumerating points on a line no further than a certain distance apart
func enumeratePointsOnLine(startPoint: CGPoint, endPoint: CGPoint, maxDistance: CGFloat, block: (point: CGPoint)->())
{
// for single point we don't iterate anything
guard !CGPointEqualToPoint(startPoint, endPoint) else
{
block(point: startPoint)
return
}
let deltaX = endPoint.x - startPoint.x
@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]