Author: Chris Lattner
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
// This extension is only available on iOS prior to 10.0. | |
// If you want to use on iOS 10.0 and later, please use ISO8601DateFormatter | |
extension DateFormatter { | |
@available(*, obsoleted: 10.0, message: "use ISO8601DateFormatter") | |
static let iso8601: DateFormatter = { | |
let formatter = DateFormatter() | |
formatter.calendar = Calendar(identifier: .iso8601) | |
formatter.locale = Locale(identifier: "en_US_POSIX") | |
formatter.timeZone = TimeZone(secondsFromGMT: 0) | |
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" |
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 BinaryInteger { | |
var isEven: Bool { | |
return self % 2 == 0 | |
} | |
} | |
arc4random_uniform(10).isEven |
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
enum ZodiacSign: Int { | |
case aries // ♈ | |
case taurus // ♉ | |
case gemini // ♊ | |
case cancer // ♋ | |
case leo // ♌ | |
case virgo // ♍ | |
case libra // ♎ | |
case scorpius // ♏ | |
case sagittarius // ♐ |
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
protocol Associatable { | |
var associatedValue: Any { | |
} | |
enum Hoge: Associatable { | |
case fuga(a: String) | |
var associatedValue: Any { | |
switch self { | |
case fuga(let a): return a |
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 Parent { | |
enum Section { | |
case hoge1 | |
case hoge2 | |
case hoge3 | |
case hoge4 | |
} | |
} | |
final class Child : Parent { |
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
let itDoesntDoWhatYouThinkItDoes = Int8(NSNumber(value: Int64.max)) | |
// -1 |
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 UITableView { | |
func register<Cell: UITableViewCell>(for cell: Cell) { | |
let nib = UINib(nibName: cell.className(), bundle: nil) | |
register(nib, forCellReuseIdentifier: cell.className()) | |
} | |
func dequeueReusableCell<Cell: UITableViewCell>(for indexPath: IndexPath) -> Cell { | |
return dequeueReusableCell(withIdentifier: Cell.className(), for: indexPath) as! Cell | |
} | |
} |
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
func something(value: Float) { | |
print("float") | |
} | |
func something(value: Double) { | |
print("double") | |
} | |
something(value: 0.1234) //double | |
something(value: 0.1234) //double |
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
func repeated<T:Integer>(word: String, count: T) -> String { | |
guard count >= 0 else { | |
return "" | |
} | |
return String(repeating: word, count: Int(count.toIntMax())) | |
} | |
repeated(word: "🐬", count: 5) |