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
| collections: | |
| drafts: | |
| output: 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
| import UIKit | |
| class TextEditing: UIViewController { | |
| @IBOutlet weak var textView: UITextView? | |
| func updateText(with newString: String?) { | |
| guard let textView = textView, newString = newString, (diffRange, changedText) = diff(textView.text, newString) else { return } | |
| guard let selectedRange = textView.selectedTextRange else { textView.text = newString; return } | |
| textView.text = newString |
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
| private func nearestPotentialStartTime(toTime time: NSDate) -> NSDate { | |
| let calendar = NSCalendar.currentCalendar() | |
| let components = calendar.components([.Era, .Year, .Month, .Day, .Hour, .Minute], fromDate: time) | |
| let minutes = components.minute | |
| let nextStartMark = (minutes % startInterval) + startInterval | |
| let previousStartMark = nextStartMark - startInterval | |
| let previousDelta = abs(minutes - previousStartMark) | |
| let nextDelta = abs(minutes - nextStartMark) |
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
| // MARK: - HEX init | |
| // From Tim Shadel | |
| enum UIColorInputError: ErrorType { | |
| case UnableToScanHexValue | |
| } | |
| extension UIColor { | |
| convenience init(hex: Int, alpha: CGFloat = 1.0) { |
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
| // From Tim Shadel | |
| enum FakeError: String, ErrorType, CustomStringConvertible { | |
| case LoginFailed | |
| var description: String { | |
| return "FakeError: \(self.rawValue)" | |
| } | |
| } |
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
| extension ErrorType { | |
| // MARK: - Internal functions | |
| func describe() -> String { | |
| if let error = self as? CustomStringConvertible { | |
| return error.description | |
| } else { | |
| let error = self as NSError | |
| return error.localizedDescription |
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
| // From Bart Whiteley | |
| public protocol UnmarshalingFactory: Marshal.ValueType { | |
| associatedtype ConvertibleType = Self | |
| static func createFromObject(object: MarshaledObject) throws -> ConvertibleType | |
| } | |
| extension UnmarshalingFactory { | |
| public static func value(object: Any) throws -> ConvertibleType { |
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
| import Security | |
| /// A wrapper around the Keychain API. | |
| public class Keychain { | |
| private let name: String | |
| private let accessGroup: String? | |
| public init(name: String, accessGroup: String? = nil) { | |
| self.name = name |
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
| class CollectionViewController: UIViewController { | |
| ... | |
| func snapToCenter() { | |
| let centerPoint = view.convertPoint(view.center, toView: collectionView) | |
| guard let centerIndexPath = collectionView.indexPathForItemAtPoint(centerPoint) | |
| collectionView.scrollToItemAtIndexPath(centerIndexPath, atScrollPosition: .CenteredHorizontally, animated: 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
| struct FlagEmoji { | |
| private static let regionalIndicatorA: UInt32 = 0x1F1E6 | |
| | |
| enum Error: ErrorType { | |
| case ExpectedOnlyAZ | |
| } | |
| | |
| private static func toRegionalIndicator(scalar: UnicodeScalar) throws -> UnicodeScalar { | |
| let offset: UInt32 | |
| switch scalar { |
NewerOlder