Last active
May 27, 2017 19:53
-
-
Save danielt1263/6141e6003e408399c2dd7a7cc266dce6 to your computer and use it in GitHub Desktop.
Swift Extensions... These are extensions to Swift standard types that I find useful.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// DictionaryExtensions.swift | |
// | |
// Created by Daniel Tartaglia on 6/18/16. | |
// Copyright © 2016 Daniel Tartaglia. MIT License. | |
// | |
extension Dictionary { | |
/// An immutable version of update. Returns a new dictionary containing self's values and the key/value passed in. | |
func updatedValue(_ value: Value, forKey key: Key) -> Dictionary<Key, Value> { | |
var result = self | |
result[key] = value | |
return result | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SequenceExtensions.swift | |
// | |
// Created by Daniel Tartaglia on 6/10/16. | |
// Copyright © 2016 Daniel Tartaglia. MIT License. | |
// | |
extension Sequence where Iterator.Element: Hashable { | |
/// returns an array of elements filtered to ensure that no elements are duplicated. | |
func unique() -> [Iterator.Element] { | |
return reduce((Set<Iterator.Element>(), Array<Iterator.Element>())) { current, name in | |
guard current.0.contains(name) == false else { return current } | |
return (current.0.inserted(name), current.1 + [name]) | |
}.1 | |
} | |
} | |
extension Sequence where Iterator.Element: Equatable { | |
/// Returns true if every element is equal to the first element. | |
/// Returns true if the sequence is empty. | |
func allEqual() -> Bool { | |
return allEqual({ $0 == $1 }) | |
} | |
} | |
extension Sequence { | |
/// Returns true if every element satisfies the predicate. | |
func all(are pred: (Iterator.Element) -> Bool) -> Bool { | |
return !contains(where: { !pred($0) }) | |
} | |
/// Returns true if every element is equal to the first element according to the pred passed in. | |
/// Returns true if the sequence is empty. | |
func allEqual(_ pred: (Iterator.Element, Iterator.Element) -> Bool) -> Bool { | |
var first: Iterator.Element? | |
for each in self { | |
if let first = first { | |
if !pred(first, each) { | |
return false | |
} | |
} | |
else { | |
first = each | |
} | |
} | |
return true | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SetExtensions.swift | |
// | |
// Created by Daniel Tartaglia on 6/10/16. | |
// Copyright © 2016 Daniel Tartaglia. MIT License. | |
// | |
extension Set { | |
/// returns a new set with `member` added. | |
func inserted(_ member: Element) -> Set { | |
var result = self | |
result.insert(member) | |
return result | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// StringExtensions.swift | |
// | |
// Created by Daniel Tartaglia on 6/10/16. | |
// Copyright © 2016 Daniel Tartaglia. MIT License | |
// | |
import Foundation | |
extension String { | |
/// Converts an NSRange across a String into a swift Range type. | |
// from http://stackoverflow.com/questions/25138339/nsrange-to-rangestring-index | |
func rangeFromNSRange(nsRange : NSRange) -> Range<String.Index> { | |
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location) | |
let to16 = utf16.index(from16, offsetBy: nsRange.length) | |
if let from = String.Index(from16, within: self), | |
let to = String.Index(to16, within: self) { | |
return from ..< to | |
} | |
fatalError() | |
} | |
func occurences(of text: String) -> Int { | |
var result = 0 | |
var index = startIndex | |
while index < endIndex, let range = range(of: text, range: index..<endIndex) { | |
result += 1 | |
index = range.upperBound | |
} | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment