Skip to content

Instantly share code, notes, and snippets.

@dkw5877
dkw5877 / Split.swift
Created February 12, 2019 15:02
Swift Standard Library split function
@inlinable
public __consuming func split(separator: Element, maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true
) -> [ArraySlice<Element>] {
return split(maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences, whereSeparator: { $0 == separator })
}
}
@inlinable
public __consuming func split(maxSplits: Int = Int.max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Element) throws -> Bool ) rethrows -> [ArraySlice<Element>] {
_precondition(maxSplits >= 0, "Must take zero or more splits")
@dkw5877
dkw5877 / BinarySearchArraySlice.swift
Created February 12, 2019 14:59
Example of Binary Search using ArraySlice
func search<T: Comparable>(target:T, slice:ArraySlice<T>) -> Int? {
let lowIndex = slice.startIndex
let highIndex = slice.endIndex - 1
while lowIndex <= highIndex {
let midIndex = slice.startIndex + (highIndex - lowIndex)/2
let value = slice[midIndex]
if target == value {
@dkw5877
dkw5877 / ContiguousArray.swift
Created February 12, 2019 14:55
Swift Standard Library functions for Map and Filter showing use of ContiguousArray
@inlinable
public func map<T>(
_ transform: (Element) throws -> T
) rethrows -> [T] {
let initialCapacity = underestimatedCount
var result = ContiguousArray<T>()
result.reserveCapacity(initialCapacity)
var iterator = self.makeIterator()
@dkw5877
dkw5877 / lexicographically.swift
Last active February 13, 2019 15:44
Swift standard library sequence function lexicographicallyPrecedes
let word1 = "bat"
let word2 = "cat"
let lex_words = word1.lexicographicallyPrecedes(word2)
print(lex_words) //result TRUE
let zebra = "zebra"
let giraffe = "giraffe"
let lex_zoo = zebra.lexicographicallyPrecedes(giraffe)
print(lex_zoo) //result FALSE
@dkw5877
dkw5877 / RegularExpression+NSString.swift
Last active February 1, 2019 20:42
Using Regular Expressions in Swift with NSString
/* sample text */
let text = "this is some text where we can replace the last part... [+6025 characters]"
/*
* define a regular expression to search for text
* see https://www.regular-expressions.info/
*/
let pattern = "\\[.*\\]"
/* create the regular expression with options */
@dkw5877
dkw5877 / RegularExpression+String.swift
Created February 1, 2019 16:35
Using RegularExpression in Swift with String
/* sample text */
let text = "this is some text where we can replace the last part... [+6025 characters]"
/*
* define a regular expression to search for text
* see https://www.regular-expressions.info/
*/
let pattern = "\\[.*\\]"
/* search the text for the regular expression, returns the Range */
let monitor = NWPathMonitor()
/* closure called when path changes */
let pathUpdateHandler = {(path:NWPath) in
let availableInterfaces = path.availableInterfaces
if !availableInterfaces.isEmpty {
//e.g. [ipsec4, en0, pdp_ip0]
let list = availableInterfaces.map { $0.debugDescription }.joined(separator: "\n")
}
+ (BOOL)checkForVPNConnectivity:(NSString *)hostName {
BOOL success = false;
if (hostName.length == 0) {
return success;
}
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
import UIKit
class ProfileViewController: UIViewController {
private var profileImage:CircularImageView = {
let imageView = CircularImageView(frame: .zero)
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
@dkw5877
dkw5877 / ScrollingContainerViewController.swift
Last active April 12, 2019 20:01
View controller who's main view has been replaced with a UIScrollView to support displaying multiple view controller's content
import UIKit
final class VerticalScrollingViewController: UIViewController {
private lazy var scrollView:UIScrollView = {
let scrollView = UIScrollView(frame:UIScreen.main.bounds)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.preservesSuperviewLayoutMargins = true
scrollView.backgroundColor = Theme.backgroundColor
return scrollView