Avatar
norio_nomura 6/10/2017 2:45 PM
if let convertibleType = type.self as? IntConvertible.Type { という書き方を知った。 import Foundation protocol IntConvertible { static func convert(from int: Int) -> Self? } extension Int { func convert<T>(to type: T.Type) -> T? { if let convertibleType = type.self as? IntConvertible.Type { return convertibleType.convert(from: self) as? T } return nil } } extension Date: IntConvertible { static func convert(from int: Int) -> Date? { return Date.init(timeIntervalSinceReferenceDate: TimeInterval(int)) } } 1.convert(to: Date.self) // "2001/01/01 9:00"