Skip to content

Instantly share code, notes, and snippets.

@sketchytech
sketchytech / Common Swift String Extensions
Last active February 3, 2021 19:03 — forked from albertbori/Common Swift String Extensions
Added and amended to use pure Swift where possible
import Foundation
extension String
{
// Works in Xcode but not Playgrounds because of a bug with .insert()
mutating func insertString(string:String,ind:Int) {
var insertIndex = advance(self.startIndex, ind, self.endIndex)
for c in string {
@sketchytech
sketchytech / Swift extension to NSMutableAttributedString (highlight and replace)
Last active February 23, 2016 07:58
Swift extension to NSMutableAttributedString adding methods for highlighting and replacing substrings
extension NSMutableAttributedString {
func highlightStrings(stringToHighlight:String, usingRegex:Bool = false) {
var useRegex:NSRegularExpressionOptions?
if !usingRegex {
useRegex = NSRegularExpressionOptions.IgnoreMetacharacters
}
@sketchytech
sketchytech / Swift Generic Functions
Last active August 29, 2015 14:05
Swift Generic Functions (that work with String and Array instances)
// Removed contains() and reduce() from function to make it less expensive
func sortedSet<S:CollectionType where S.Generator.Element: Comparable>(seq:S) -> [S.Generator.Element] {
var set = sorted(seq){$0<$1}
var inds = [Int]()
for s in enumerate(set) {
if s.index > 0 && s.element == set[advance(set.startIndex, s.index-1,set.endIndex)] {
inds.append(s.index-1)
}
}
@sketchytech
sketchytech / Rough XML Parser for Swift
Last active August 29, 2015 14:05
This is just for fun and only currently works with very simple xml, no attributes
protocol XMLDelegateClass {
func tagClosed(string:String)
func tagOpen (string:String)
func stringFound (string:String)
}
@sketchytech
sketchytech / Calculating a count for UTF16 string with unicode scalar characters - use utf16Count
Last active September 17, 2015 20:58
Working with bytes and strings in Swift (Unicode Scalar, UTF8 and UTF16)
newStr = "hello 😂😒😡"
// the number of elements:
let count = newStr.utf16Count
// create array of appropriate length:
var array = [UInt16](count: count, repeatedValue: 0)
for a in enumerate(newStr.utf16) {
@sketchytech
sketchytech / gist:a691de7f40ee3202ac86
Created September 7, 2014 12:46
Type methods and properties
class SomeClass {
class var description:String {
return "computed class property"
} // ok
class var property:String = "class property" // compiler warning: class variables not yet supported
class func typeMethod() -> String {
return "type method"
} // ok
@sketchytech
sketchytech / Code File 1
Created September 12, 2014 19:07
#Swiftlang pub quiz: identify the file that will crash; bonus points if you can identify the most memory-friendly file.
import UIKit
class ViewController: UIViewController {
var a:Foo? = Foo()
override func viewDidLoad() {
super.viewDidLoad()
var b = a!.myFunc
a = nil
println(b())
@sketchytech
sketchytech / gist:7e7c48f5bbce1bb61228
Last active August 29, 2015 14:09
Swift: Translation and Rotation of CGContext (iOS/Xcode)
// This code accompanies the following post: http://sketchytech.blogspot.co.uk/2014/11/swift-translating-and-rotating.html
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let newView = View(frame: CGRect(x: 0, y: 0, width: CGRectGetWidth(self.view.frame), height: CGRectGetHeight(self.view.frame)))
@sketchytech
sketchytech / gist:e116fd61c4f29844d360
Created November 9, 2014 20:28
Swift: Layer hit-testing
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var blueLayer = CALayer()
@sketchytech
sketchytech / gist:d0a8909459aea899e67c
Last active March 30, 2021 13:47
Swift: Draw a clock face (part 1)
// accompanies this blogpost http://sketchytech.blogspot.co.uk/2014/11/swift-how-to-draw-clock-face-using.html
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.