Назначенный инициализатор (designated initializer) - это главный инициализатор(конструктор), все остальные методы
создающие класс вызывают этот метод.
Как выглядит назначенный инициализатор?
enum TimePeriod { | |
case Seconds(Int) | |
case Minutes(Int) | |
case Hours(Int) | |
var timeInterval: NSTimeInterval { | |
switch self { | |
case .Seconds(let value): return NSTimeInterval(value) | |
case .Minutes(let value): return NSTimeInterval(value * 60) | |
case .Hours(let value): return NSTimeInterval(value * 60 * 60) |
public extension Int { | |
public var seconds: DispatchTimeInterval { | |
return DispatchTimeInterval.seconds(self) | |
} | |
public var second: DispatchTimeInterval { | |
return seconds | |
} | |
import Foundation | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
/// define an operation success or error result | |
enum Result<T> { | |
case error(Error) | |
case success(T) | |
} |
FWIW: I (@rondy) am not the creator of the content shared here, which is an excerpt from Edmond Lau's book. I simply copied and pasted it from another location and saved it as a personal note, before it gained popularity on news.ycombinator.com. Unfortunately, I cannot recall the exact origin of the original source, nor was I able to find the author's name, so I am can't provide the appropriate credits.
import UIKit | |
extension UITableView { | |
func registerNib<T: UITableViewCell>(forCellType type: T.Type) { | |
let name = String(describing: type) | |
let nib = UINib(nibName: name, bundle: nil) | |
register(nib, forCellReuseIdentifier: name) | |
} | |
func registerNib<T: UIView>(forHeaderFooterType type: T.Type) { |
import Foundation | |
import UIKit | |
// Usage Examples | |
let shadowColor = Color.shadow.value | |
let shadowColorWithAlpha = Color.shadow.withAlpha(0.5) | |
let customColorWithAlpha = Color.custom(hexString: "#123edd", alpha: 0.25).value | |
enum Color { | |
import Foundation | |
import UIKit | |
// Usage Examples | |
let system12 = Font(.system, size: .standard(.h5)).instance | |
let robotoThin20 = Font(.installed(.RobotoThin), size: .standard(.h1)).instance | |
let robotoBlack14 = Font(.installed(.RobotoBlack), size: .standard(.h4)).instance | |
let helveticaLight13 = Font(.custom("Helvetica-Light"), size: .custom(13.0)).instance | |
struct Font { |
import Foundation | |
extension Date { | |
func toString(format: String = "yyyy-MM-dd") -> String { | |
let formatter = DateFormatter() | |
formatter.dateStyle = .short | |
formatter.dateFormat = format | |
return formatter.string(from: self) | |
} |
// This came from StackOverflow | |
// http://stackoverflow.com/questions/24857986/load-a-uiview-from-nib-in-swift | |
// | |
// https://gist.github.com/karnlund/b666acc88532d5f1ca591dd3580605cc | |
import UIKit | |
public extension UIView { | |
public class func fromNib(_ nibName: String? = nil) -> Self? { | |
return loadFromNib(nibName) |