This file contains 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
protocol HasMiddleValue { | |
typealias ItemType | |
func middle() -> [ItemType] | |
} | |
extension Array: HasMiddleValue { | |
typealias ItemType = Generator.Element |
This file contains 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 Array where Element: UIView { | |
/** | |
Sorts an array of `UIView`s or subclasses by `tag`. For example, this is useful when working with `IBOutletCollection`s, whose order of elements can be changed when manipulating the views in Interface Builder. Just tag your views in Interface Builder and then call this method on your `IBOutletCollection`s in `viewDidLoad()`. | |
- author: Scott Gardner | |
- seealso: | |
* [Source on GitHub](http://bit.ly/SortUIViewsInPlaceByTag) | |
*/ | |
mutating func sortUIViewsInPlaceByTag() { | |
sortInPlace { (left: Element, right: Element) in |
This file contains 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 | |
/** | |
Returns an array of `CGPoint`s that are evenly distributed around the circumference of a circle for a number of points, center x, center y, and radius of the circle, and maximum number of decimal points precision for the x and y values. | |
- author: Scott Gardner | |
- parameter numberOfPoints: the number of points to plot; 1 or more | |
- parameter centerX: the center `x` of the circle | |
- parameter centerY: the center `y` of the circle | |
- parameter radius: the radius of the circle (distance from center) | |
- parameter precision: the maximum number of decimal places precision, 0 or higher, **defaults to 3** |
This file contains 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 | |
extension UIStoryboard { | |
enum Name: String { | |
case main = "Main" | |
case otherStuff = "OtherStuff" | |
} | |
This file contains 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
// | |
// HandlesKeyboard.swift | |
// | |
// Created by Scott Gardner on 1/14/19. | |
// Copyright © 2019 Scott Gardner. All rights reserved. | |
// | |
import UIKit | |
protocol HandlesKeyboard where Self: UIViewController { |
This file contains 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 Foundation | |
/** | |
Converts any numeric literal (e.g., 1) or string containing a numeric literal (e.g., "1"), into a spelled-out number string (e.g., "one"). [Source on GitHub](http://bit.ly/SwiftSpellOutNumber) | |
- parameter number: a numeric literal, or string containing a numeric literal | |
- returns: String? | |
*/ | |
public func spellOut<N>(number: N) -> String? { | |
let formatter = NumberFormatter() | |
formatter.numberStyle = .spellOut |
This file contains 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
func ==(x: HashableType, y: HashableType) -> Bool { | |
return x.isEqual(y.value) | |
} | |
struct HashableType: Hashable { | |
let value: Any | |
var hashValue: Int { | |
return getHashValue() | |
} |
This file contains 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
// Objective-C | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if ([segue.identifier isEqualToString:@"Item Detail"]) { | |
DetailViewController *d = (DetailViewController *)segue.destinationViewController; | |
d.title = segue.identifier; | |
d.itemForDetail = item; | |
} else if ([segue.identifier isEqualToString:@"Related Items"]) { | |
UINavigationController *nc = segue.destinationViewController; |
This file contains 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 String { | |
/** | |
Checks if string is a palindrome, ignoring spaces and capitalization. [Source on GitHub](http://bit.ly/SwiftStringPalindromeExtension) | |
- note: | |
[What's a palindrome?](https://en.wikipedia.org/wiki/Palindrome) | |
- returns: Bool | |
*/ | |
public func isPalindrome() -> Bool { | |
var s = String(self.characters.filter { $0 != Character(" ") }).lowercased() |
This file contains 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 Swift 2 Essential Training http://www.lynda.com/Swift-tutorials/Using-labels/422096/447960-4.html | |
var i = 0 | |
let hello = "Hello, playground!" | |
start: do { | |
i++ | |
do { | |
print(hello) |
NewerOlder